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?
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
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.