淺談C記憶體配置2

來源:互聯網
上載者:User

記憶體配置方式有三種:
(1) 從靜態儲存地區分配。記憶體在程式編譯的時候就已經分配好,這塊記憶體在程式的
整個運行期間都存在。例如全域變數,static 變數。
(2) 在棧上建立。在執行函數時,函數內局部變數的儲存單元都可以在棧上建立,函
數執行結束時這些儲存單元自動被釋放。棧記憶體配置運算內建於處理器的指令集
中,效率很高,但是分配的記憶體容量有限。
(3) 從堆上分配,亦稱動態記憶體分配。程式在啟動並執行時候用malloc 或new 申請任意多
少的記憶體,程式員自己負責在何時用free 或delete 釋放記憶體。動態記憶體的生存期
由我們決定,使用非常靈活,但問題也最多。

void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
請問運行Test 函數會有什麼樣的結果?
答:程式崩潰。
因為GetMemory 並不能傳遞動態記憶體,
Test 函數中的 str 一直都是 NULL。
strcpy(str, "hello world");將使程式崩
潰。
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
請問運行Test 函數會有什麼樣的結果?
答:可能是亂碼。
因為GetMemory 返回的是指向“棧記憶體”
的指標,該指標的地址不是 NULL,但其原
現的內容已經被清除,新內容不可知。
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
請問運行Test 函數會有什麼樣的結果?
答:
(1)能夠輸出hello
(2)記憶體流失
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
請問運行Test 函數會有什麼樣的結果?
答:篡改動態記憶體區的內容,後果難以預
料,非常危險。
因為free(str);之後,str 成為野指標,
if(str != NULL)語句不起作用。
**********************************************************************************************

void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100); // 注意參數是 &str,而不是str
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
如果非得要用指標參數去申請記憶體,那麼應該改用“指向指標的指標”.由於“指向指標的指標”這個概念不容易理解,我們可以用函數傳回值來傳遞動態記憶體。
char *GetMemory3(int num)
{
char *p = (char *)malloc(sizeof(char) * num);
return p;
}
void Test3(void)
{
char *str = NULL;
str = GetMemory3(100);
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
**********************************************************************************************

用函數傳回值來傳遞動態記憶體這種方法雖然好用,但是常常有人把return 語句用錯
了。這裡強調不要用return 語句返回指向“棧記憶體”的指標,因為該記憶體在函數結束時
自動消亡

char *GetString(void)
{
char p[] = "hello world";
return p; // 編譯器將提出警告
}
void Test4(void)
{
char *str = NULL;
str = GetString(); // str 的內容是垃圾
cout<< str << endl;
}
用調試器逐步跟蹤Test4,發現執行str = GetString 語句後str 不再是NULL 指標,
但是str 的內容不是“hello world”而是垃圾。

char *GetString2(void)
{
char *p = "hello world";
return p;
}
void Test5(void)
{
char *str = NULL;
str = GetString2();
cout<< str << endl;
}
函數Test5 運行雖然不會出錯,但是函數GetString2 的設計概念卻是錯誤的。因為
GetString2 內的“hello world”是常量字串,位於靜態儲存區,它在程式生命期內
恒定不變。無論什麼時候調用GetString2,它返回的始終是同一個“唯讀”的記憶體塊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.