0

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...)

2 Answers 2

4

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.

0

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.