简体   繁体   中英

Heap break does not change after malloc

#include <stdio.h>
#include <errno.h>
#include <sys/resource.h>

int main(int argc, char *argv[]) {
    printf("main: %p\n", main);
    printf("brk before malloc: %p\n", sbrk(0));
    int *a = malloc(sizeof(int) * 100);
    printf("malloc at %p\n", a);
    printf("brk after malloc: %p\n", sbrk(0));
}

Output:

main: 0x108f9ae90
brk before malloc: 0x8fbc000
malloc at 0x7fb380c02b40
brk after malloc: 0x8fbc000

Why break address does not change after malloc? In addition, why is the top of the heap (break) address so far away from malloc'ed address, and it's even lower than the address of main, which is in the text segment?

If I understand correctly, the return value of the second sbrk call should have the largest value among all because it's the address of the top of the heap, and heap data should have a higher address than text segment. I was expecting 2nd brk > malloc'ed addr > 1st brk > main, but apparently it's not the case here.

The current Mac OS X implementation of sbrk is an emulation, and has a maximum allocation of 4 megabytes. See here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM