how free() method know where to stop deallocated memory

Sep 28, 2010 at 1:21pm
Hi All...

I have a question on free() method.
How free() method know until what address to stop freeing memory ?
Let says I allocated 4 bytes of memory using void: *p = malloc(4)
If I will free(p), the allocated memory will be free again.
My question is how free() method know to stop deallocating memory until 4 bytes?
is there some kind of NULL so free() method stop when occur it?

Best Regards
YyYo


Sep 28, 2010 at 1:36pm
malloc may store somewhere the information of how much memory was allocated
in "The C programming language" there's an example implementation of malloc and free that explains this
Sep 28, 2010 at 2:21pm
Typical memory management allocators will allocate slightly more memory than you request, then store
information about the allocation (such as the size) in the first few bytes and return you a pointer just past
that data. When you free the pointer, it looks in the few bytes before the pointer to get the size of the buffer.
Sep 28, 2010 at 2:58pm
... which is why buffer overruns are catastrophic.
Topic archived. No new replies allowed.