Problems with malloc

80 Views Asked by At
int main(int argc, char ** argv)
{
    char *p[10];
    
    for(int i =0 ; i < 10; ++i)
    {
        ssize_t msize = pow(2,i) * sizeof(char);
        p[i] = (char*)malloc(msize);
        printf("%p\n",sbrk(0));
    }
    printf("malloc done!\n");
    
    for(int i =0 ;i < 10; ++i)
    {
        free(p[i]);
    }

    exit(0);
}

The output like that:

0x55ffd4dd5000
0x55ffd4dd5000
0x55ffd4dd5000
0x55ffd4dd5000
0x55ffd4dd5000
0x55ffd4dd5000
0x55ffd4dd5000
0x55ffd4dd5000
0x55ffd4dd5000
0x55ffd4dd5000
malloc done!

When using malloc() to allocate the blocks of memory smaller than MMAP_THRESHOLD bytes, the glibc malloc() will use sbrk() or brk() to finish that. As known, both brk() and sbrk() will adjust the size of the heap.

So if I use malloc() to allocate the small(size < MMAP_THRESHOLD) memory block, the heap should be changed, But why does heap not change in this case?

0

There are 0 best solutions below