c - If I malloc(1) twice in a row, why do the values differ by 32? -
in c, if run malloc(1)
twice , print out 2 values casted uintptr_t
, values differ 32. example,
void * last = malloc(1); void * next = malloc(1); printf("orig %lu\n", ((uintptr_t)last)); printf("next %lu\n", ((uintptr_t)next));
prints out
orig 30572560 next 30572592
malloc(size_t size)
allocates size bytes, according this. expect value increase 8 when malloc
1 byte, another. why differ 32 instead? thanks!
this implementation detail differ between different compilers/standard libriaries. in general, there no guarantee subsequent malloc
calls return adjacent memory regions. note each memory region associated additional metadata, including not limited region size (otherwise wouldn't possible know how memory deallocate when free
run). metadata put inside of allocated region, making bigger requested. there other factors influence how memory allocated - memory alignment , "hole" finding algorithm used malloc
implementation, etc.
Comments
Post a Comment