If you'd like to see under the hood of a standard memory allocator, well, here you go. This is the magic that makes an allocator work - the ability to split chunks of memory and to rejoin them again, and it all fits on one page.
What you looking is a simple linked list, but what you can't see is the space between the nodes, which is where all of the free memory is and, if you're using any kind of modern system with memory allocation, where all of your data is.
The size of the Node structure determines the overhead associated with malloc, and it's significant - 16 bytes per chunk in this example, and for systems that can merge in both directions (this one can only go forward), 24 bytes is common.
If you're doing a lot of small allocations, I would recommend an efficient, high performance custom allocator or a power of two memory allocator, which is in fact what I'm working on. But what's funny is that even a power of two allocator needs an internal general purpose allocator to function (for its internal arrays) which is why I had to write this "normal allocator" code.
#cplusplus #malloc #memoryallocation