Guillaume
Guillaume

Reputation: 191

Free a memory block in the heap

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

Answers (2)

Viktor Simkó
Viktor Simkó

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

Mohan
Mohan

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

Related Questions