Search code examples
cmallocfreerealloc

Malloc, free, and realloc behaviour in C


I have to recode these 3 functions in C and the goal is that the recoded functions have to replace the system functions in different programs or system calls.

I don't really understand what is the behaviour of these functions since I've read the mans, I just know I'm allowed to use brk() and sbrk().


Solution

  • malloc basically pre allocates memory pages (Cf getpagesize(2)), and returns pointer to usable parts of those pre allocated areas when called. malloc then have to "remember" which part of those pre allocated memory segments are being used, and which one are free. Reading this article: https://www.cocoawithlove.com/2010/05/look-at-how-malloc-works-on-mac.html might help! (I know it was great help to me when i re-coded malloc on mac Os as an university assignment ;)).