为什么free()不会减少程序数据空间

时间:2017-09-27 14:49:37

标签: c linux malloc free sbrk

我的理解是malloc内部使用sbrk()而sbrk(0)指向程序中断的当前位置。然后根据以下代码: -

#include<stdio.h>
#include<malloc.h>
int main()
{
    printf("Before allocation :- %u\n",sbrk(0));

    int *ptr = malloc(sizeof(int)*100);
    printf("After Allocation of 400Bytes :- %u\n",sbrk(0));

    free(ptr);
    printf("After free() :- %u\n",sbrk(0));

    return 0;
}

输出是:

Before allocation :- 37367808
After Allocation of 400Bytes :- 37502976
After free() :- 37502976

但是在调用free()后,它应该再次打印37367808而不是打印37502976

0 个答案:

没有答案