c - malloc storing its metadata -
surprisingly both programs gave difference between 2 pointers same though info types different.....
how malloc store meta info trying find out little experiment...
program 1 :
int main () { char *i,*j; i=(char*)malloc (sizeof(char)); j=(char*)malloc (sizeof(char)); printf ("%x\n",i); printf ("%x\n",j); homecoming 0; }
output :
710010 710030
program 2 :
int main () { int *i,*j; i=(int*)malloc (sizeof(int)); j=(int*)malloc (sizeof(int)); printf ("%x\n",i); printf ("%x\n",j); homecoming 0; }
output :
16b8010 16b8030
what had in mind before programme :
| meta info of | memory space of | meta info of j | memory space of j |
but results don't back upwards theory....
malloc
"rounds up" allocations convenient size set @ compile time library. causes subsequent allocations , deallocations fragment memory less if allocations created match requests.
where malloc
stores metadata not why values both 0x20 "apart". can read on 1 method of implementing malloc
(and friends) here; see slides 16 , 28.
imagine case of string manipulation program, lots of different sized allocations occurring in "random" order. tiny "left over" chunks develop leaving totally useless bytes of memory spread out between used chunks. malloc
prevents satisfying memory requests in multiples of minimum size (apparently 0x20 in case). (ok, technically request 0x1e bytes, there 2 bytes of "wasted" space left on , unused after request. since malloc
allocates 0x20 bytes instead of 0x1e, there not ever 2-byte fragment left over. because metadate malloc
bigger 2-bytes, there no way maintain track of bytes.)
c malloc details
No comments:
Post a Comment