2012-05-07 2 views
0

malloc()은 over allowed이고 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 페이지를 할당하고 free() 이후에 나머지가 아닌 전체를 해제하는 이유는 무엇입니까?

+0

33 개가 어디서 나왔습니까? 나는 당신의 결과에서 그것을 볼 수 없다 ... – RedX

+0

죄송합니다. 내가 틀린 코드였습니다. 난 그냥 게시물을 업데이 트 RedX –

답변

4

컴퓨터는 메모리 셀에 저장된 내용을 잊어 메모리를 해제합니다. free()를 입력하면 해당 주소에 아무 것도 저장되지 않고 메모리 셀이 더 이상 예약되어 있지 않음을 간단하게 힙 관리에 알릴 수 있습니다. 따라서 메모리 셀을 "지우는"것과 같은 것은 없습니다. 코드를 적극적으로 쓰는 코드는 실행되지 않습니다.

구체적으로 free()를 호출하고 3xsizeof (int) 바이트를 나머지 프로그램에서 자유롭게 사용할 수 있다고 알려줍니다. 프로그램의 다른 부분은 값 0을 어딘가에 저장할 필요가 있었고 이전에 예약했던 셀에 있었던 것입니다.

일반적으로 free()에 전달 된 포인터의 내용을 인쇄하는 것은 정의되지 않은 동작입니다. 아무거나 일어날 수있다. 특정 컴파일러에 대한 특정 프로그램에서 정의되지 않은 동작이 특정 방식으로 작동하는 이유를 파악하려면 무의미한 지식이 필요합니다. free() 호출 후에는 메모리 셀을 신뢰할 수 없다는 것을 받아 들일 만하면됩니다.

+1

강하게 관련이 또한 [이 서사시 답변] (http://stackoverflow.com/a/6445794/1025391)입니다. – moooeeeep

+0

첫 번째 문장은 매우 정확합니다 - 거기에 저장된 것을 잊지 않습니다. 그래서 85와 78이 다시 튀어 나오고 있습니다. –

+0

@Agent_L 글쎄, "컴퓨터"란 무슨 뜻인지 정의하는 것입니다. 귀하의 프로그램과 귀하의 CPU가 확실히 그것에 대해 모든 것을 잊었습니다, 거기에 남아있는 데이터에 대한 참조가 없습니다. RAM 셀은 잊지 않았습니다. 옛날부터 오래된, 진부한 RAM이 있었다면, 나는 컴퓨터가 셀의 전압 리프레쉬를 건너 뛰도록 자유로 웠을 것이라고 생각한다. 그런 다음 당신이 그들을 읽으려고한다면, 당신은 단지 임의의 쓰레기를 얻을 것이다. – Lundin