If malloc calls VirtualAlloc() function to allocate memory (which allocates minimum 4Kb), how malloc allocates 4 bytes for int?
malloc
requests memory from the OS in multiples of the page size (obviously, since the page size is by definition the quantum of allocated memory) and hands it out to you in smaller chunks.
That's not different than what all memory allocators do -- in fact, specialized memory allocators (e.g. Boost.Pool
) that use malloc
behind the scenes do exactly this once more: they allocate a bigger chunk of memory through malloc
and hand it out to you in smaller pieces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With