非常基本的概念疑問。 請不要恨我這個愚蠢的問題如何從線程調用的函數返回
如果我們在我的主要功能
char * function()
{
char message[10];
.................
....do sth ......
return message;
}
調用一個函數像下面從一個線程在這種情況下,內部緩衝區是自動的,一旦消失的線程函數返回。
但在這樣做它的工作原理
char * function()
{
char * message = (char*)malloc(10);
.................
....do sth ......
return message;
}
我感到困惑與下面的線。這是如何解決問題的?
Each thread will allocate a different array and store the address of that array in a stack variable. Every thread has its own stack so automatic data objects are different for each thread.
第一個功能無效。你不應該返回一個指向堆棧變量的指針。基本上這兩個例子涉及的是堆棧與堆內存分配。 – Corbin 2012-04-27 00:03:17
您不能將指針返回到堆棧上的某個東西。 – James 2012-04-27 00:04:03