Why does the free() function know about the size of a malloced variable?

Viewed 61

How does free() function work? We pass an only pointer on the first element of the array or string for free and this function "know" about the right border of allocated memory and freed it correctly. But why can't get this information from any C- standard functions?

1 Answers

Usually the allocated dynamically block of memory contains a prefix where the size of the allocated memory is stored.

So the function free can access this prefix to know the size of the deallocated block of memory.

Related