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

Consider the following prototype of free() function which is used to free memory allocated using malloc() or calloc() or realloc().





void free(void *ptr);


Note that the free function does not accept size as a parameter. How does free() function know how much memory to free given just a pointer?

Following is the most common way to store size of memory so that free() knows the size of memory to be deallocated.
When memory allocation is done, the actual heap space allocated is one word larger than the requested memory. The extra word is used to store the size of the allocation and is later used by free( )

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.