In C/C++ when you want to dynamically allocate memory usually call malloc void* malloc (size_t size);
which returns a pointer to a memory block of size bytes.
Once you are done using this memory bloc, you call free()
to free the memory back to the heap.
That's all fine, but what happens if you happen to call realloc void* realloc (void* ptr, size_t size);
which changes the size of the memory block pointed by ptr. You still call free() when you are done using the memory, but my question is how does the compile know how much memory to free?
Intuitively I can come up with an answer, but I'm interested in implementation details - how is it really done? is it compiler dependent? it is part of a standard?
Thanks in advance!