開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
開動態記憶體時,每次都要用free釋放嗎?
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
void main()
{
int *a;
int i;
for (i=0;i<100;i++)
function();
a = (int*) malloc(100*sizeof(int));
return;
}
void function()
{
int *b;
b = (int*) malloc(1000*sizeof(int));
free(b);
return;
}
補充說明(Supplement):
如上程式,function 中的 b 要用free,不然每次for loop跑時,他都會重開一次記憶
體。
但在 main 中的a要free嗎?這是我主要的問題。如果我不free的話,他會不會
block住我的記憶體,以至於我要重開機才能使用那塊記憶體?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 128.138.65.69
推
02/08 11:00, , 1F
02/08 11:00, 1F
→
02/08 11:00, , 2F
02/08 11:00, 2F
→
02/08 11:01, , 3F
02/08 11:01, 3F
→
02/08 11:01, , 4F
02/08 11:01, 4F
→
02/08 12:02, , 5F
02/08 12:02, 5F
→
02/08 15:56, , 6F
02/08 15:56, 6F
推
02/08 15:59, , 7F
02/08 15:59, 7F
→
02/09 13:49, , 8F
02/09 13:49, 8F
→
02/09 13:49, , 9F
02/09 13:49, 9F
推
02/09 13:50, , 10F
02/09 13:50, 10F
→
02/09 13:52, , 11F
02/09 13:52, 11F
推
02/09 18:36, , 12F
02/09 18:36, 12F