我的理解是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
。