2012-05-07 97 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