Reputation: 191
I'm currently working on a school project, and i try to understand the operations behind the free()
function.
malloc
will allocate a block in the heap.
If we want to free this block, will this block be totally set at '0'
values (with brk usage)
or
will it only be set as 'available' to allow erasement on it (data replacement...)
Upvotes: 0
Views: 946
Reputation: 2637
free
only deallocates the memory, it doesn't fill it with 0
-s.
After freeing it, writing to, or reading from that memory will cause undefined behaviour.
Upvotes: 4
Reputation: 1901
In addition to @Viktor Simkó answer free
will remove the header and add it back to the free memory list. If it forms a larger block with the surrounding free blocks these will be added together to give a larger block when user demands larger block. If a whole page is now free the allocator will, most likely, return the page to the OS.
For more information have look on How do malloc() and free() work?
Upvotes: 0