how does dynamic memory allocation work

This question already has an answer here:


To free memory, you don't generally need to say how much memory you want to free. The allocator already knows how much it gave you, and remembers that. The address of the allocated memory serves as a sort of "key" in the allocator's internal bookkeeping data structures. So all you need to do is give the address of the memory you want to free, and the allocator knows which memory you're talking about.

To be clear: there's no such thing as "partially freeing" some memory. You can't point into the middle of some allocated memory and say "free 100 bytes here". You release precisely what you acquired, no more and no less.

Finally, realloc is just a combination of malloc and memcpy, if you will, perhaps with a small optimization opportunity that allows an existing allocation to "grow" without moving the data. But the idea is the same.


 ? free function not working in c / objective-c
 ? function to free memory of 1D Array
 ? How operator new knows that memory is allocated
 ? Freeing memory in C or C++
 ? freeing memory using free()
 ? How does C free() work?
 ? What if, memory allocated using malloc is deleted using delete rather than free
 ? C: malloc(), free() and then again malloc() does work same always?
 ? Heap corruption while freeing memory
 ? Heap corruption while freeing memory