At the OS level (at least in the Unix model), your program has one large area of memory for program text, initialized and uninitialized data, and a heap for dynamically distributed data. (The stack is separate.) You can adjust the size of this region using brk and sbrk , but you cannot change it, and it is always contiguous. The vast majority of programs that deal with dynamic memory allocation require something more flexible.
malloc , free and realloc are C library functions that give you something more flexible. Under them, they get memory from the OS, calling brk and / or sbrk , but then they do additional processing to allocate (a) any number of fragments (b) of different sizes and which you can (c) individually return to the pool when you are done with them, and by the way (d) resize.
But when you return memory to the pool using free , it usually just returns to the pool, which future calls to malloc your program will draw; memory, as a rule, does not return back to the OS.
(Sorry for the lack of sample code, I don't have time for this just now.)
source share