How does free() know the size of memory to be deallocated?


The free() function is used to deallocate memory while it is allocated using malloc(), calloc() and realloc(). The syntax of the free is simple. We simply use free with the pointer. Then it can clean up the memory.

free(ptr);

The free() is not taking any size as parameter, but only pointer. So the question comes, that how the free() function know about the size of the block to deallocate?

When we use the dynamic memory allocation techniques for memory allocations, then this is done in the actual heap section. It creates one word larger than the requested size. This extra word is used to store the size. This size is used by free() when it wants to clear the memory space.

raja
Published on 26-Apr-2019 12:39:35
Advertisements