2012-05-07 93 views
0

我知道malloc()是一個over allow和free()並不釋放整個內存。我測試,檢查所有這些代碼:動態分配 - 內存管理

extern char _end; 
#include <stdlib.h> 
#include <stdio.h> 
int main(int argc, char *argv[]) 
{ 

    char * res; 
int tas,rupt,esp; 
printf("\n Before any memory reservation\n --------------------------\n"); 
tas=sbrk(0); 
printf("End position of the pile : %p\n",tas); 
rupt=&_end; 
printf("Breaking point : %p\n",rupt); 
esp=tas-rupt; 
printf("Space between the end of the heap and breaking point : %d bytes \n",esp); 
printf("Pages allocated for malloc %f\n",(tas-rupt-esp)/4096.0); 
printf("whether %d bytes\n",tas-rupt-esp); 



int nbEntier = 0, i = 0; 
int* tabEntier ; // This pointer is used as a table after the call of malloc 

// Asked the number of integers to the user 
printf("How integer do you want to store? "); 
scanf("%d", &nbEntier); 

if (nbEntier > 0) 
{ 
    tabEntier = malloc(nbEntier * sizeof(int)); 
    if (tabEntier== 0) 
    { 
     exit(0); // 
    } 

    //We ask the integer 
    for (i = 0 ; i < nbEntier ; i++) 
    { 
     printf("Integer n° %d ? ", i + 1); 
     scanf("%d", &tabEntier[i]); 
    } 

    // We display the integer one to one 
    printf("\n\nYour integers are :\n"); 
    for (i = 0 ; i < nbEntier ; i++) 
    { 
     printf("%d \n", tabEntier[i]); 
    } 
tas=sbrk(0); 
printf("End position of the pile : %p\n",tas); 
rupt=&_end; 
printf("Breaking point : %p\n",rupt); 
printf("Space between the end of the heap and breaking point : %d bytes \n",tas-rupt); 
printf("Pages allocated for malloc %f\n",(tas-rupt-esp)/4096.0); 
printf("whether %d bytes\n",tas-rupt-esp); 

    free(tabEntier); 


    printf("\nDisplay after free():\n"); 


    printf("\n\nYour integers are :\n"); 
    for (i = 0 ; i < nbEntier ; i++) 
    { 
     printf("%d \n", tabEntier[i]); 
    } 



} 

return 0; 
} 

我的結果:

 
Before any memory reservation 
-------------------------- 
End position of the pile : 0x9886000 
Breaking point : 0x8049c2c 
Space between the end of the heap and breaking point : 25412564 bytes 
Pages allocated for malloc 0.000000 
whether 0 bytes 
How integer do you want to store? 3 
Integer n° 1 ? 45 
Integer n° 2 ? 85 
Integer n° 3 ? 78 


Your integers are : 
45 
85 
78 
End position of the pile : 0x98a7000 
Breaking point : 0x8049c2c 
Space between the end of the heap and breaking point : 25547732 bytes 
Pages allocated for malloc 33.000000 
whether 135168 bytes 

Display after free(): 


Your integers are : 
0 
85 
78 

我不明白!它分配33頁,即使我問一個整數,所有的時間也是爲什麼後免費()它釋放整個第一,而不是休息..

+0

33從哪裏來?我沒有看到你的結果... – RedX

+0

對不起,我錯了代碼。我只是更新了帖子,謝謝RedX –

回答

4

的計算機被忘記什麼是存儲在一個存儲單元中釋放內存。當你鍵入free()時,你可以簡單地告訴堆管理器,該地址沒有任何感興趣的內容,並且內存單元不再被保留。所以不存在「擦除」存儲器單元這樣的事情,沒有代碼將被執行而主動將零寫入它。

在特定情況下,你調用free(),並告訴它3xsizeof(INT)字節的可自由通過程序的其他部分使用。該程序的某些其他部分需要將值0存儲在某處,並且恰好在您之前保留的單元格中。

通常,打印已經被傳遞到釋放(一個指針的內容)是未定義的行爲。任何事情都可能發生。要詳細說明爲什麼一個未定義的行爲以某種方式發揮作用,在某個特定編譯器的某個程序中,就是毫無意義的知識。簡單地接受在free()調用後內存單元不可信,這就夠了。

+1

強烈關聯也是[這個史詩般的答案](http://stackoverflow.com/a/6445794/1025391)。 – moooeeeep

+0

第一句話是相當不正確的 - 它不會忘記那裏存儲的內容。這就是爲什麼85和78會再次出現。 –

+0

@Agent_L嗯,這是一個定義你的意思是「計算機」。你的程序和你的CPU肯定已經忘記了這一切,沒有提到剩餘的數據。 RAM單元沒有被遺忘。如果從古代開始就是一些古老而蹩腳的RAM,我想電腦可以自由地跳過電池的電壓刷新,如果你試圖閱讀它們,那麼你會得到隨機的垃圾。 – Lundin