0

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

4

0 回答 0