Junior — Middle
How does the free system call determine the amount of memory to be freed?
sobes.tech AI
Answer from AI
The system call free does not accept information about the size of the memory to be freed directly. Instead, when memory is allocated via malloc or similar functions, the allocator stores administrative data (such as the block size) in metadata next to the allocated memory block. When free is called, the allocator uses the pointer to the block to find this metadata and determine the size of the block for proper deallocation.
For example, in an implementation based on ptmalloc (used in glibc), a header with information about the block size is stored before the allocated memory area. This allows free to know how much memory to free without explicitly passing the size.