Search code examples
c++memory-managementwin32-process

What kind of memory does VirtualAlloc/Ex allocate


I know that C/C++ style programs have memory sections, the stack, heap, .text, etc. But when I use VirtualAlloc, from where does it allocate memory? I don’t think it’s the heap because I can just use HeapAlloc.

Advice would be appreciated!


Solution

  • Heap memory resides in your programs virtual memory that is allocated by VirtualAlloc behind the scenes. However, the problem is that VirtualAlloc allocates memory only in large chunks called pages, which would make it infeasible to use for general memory allocation.

    For this reason there is HeapAlloc which manages allocated pages and can concatenate multiple allocations into one page so that you don't unnecessarilly allocate a whole page (usually 4KB, but this is not a rule) for every small allocation.