Handmade Hero»Forums»Code
Juan Prada
5 posts
VirtualAlloc and 32 bit Memory Space?
Edited by Juan Prada on
Hi guys, I am going through day 158 of the Handmade Hero videos, and Casey is saying that it is possible to just use VirtualAlloc/VirtualFree to handle your memory in the game, the only issue would be when building and executing the 32 bit version of the game.

Can anyone explain to me why using VirtualAlloc in a 32 bit architecture is not such a good idea?.
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
VirtualAlloc and 32 bit Memory Space?
I think that in 32bit virtual allloc can only allocate maximum 2 GB of memory.
And the base address location of memory has to be changed.
Correct me if I am wrong.
Mārtiņš Možeiko
2562 posts / 2 projects
VirtualAlloc and 32 bit Memory Space?
Edited by Mārtiņš Možeiko on
Using VirtualAlloc in 32-bit OS is perfectly fine. In fact on Windows that is pretty normal way how to allocate memory from OS in your application. malloc/new and other C/C++ functions allocate memory by using VirtualAlloc.

What Casey mean why it is not such a good idea is that because he is allocating in pretty large chunk - gigabytes. For 32-bit application max memory it can use is 4GB (max value addressable by 32-bit pointer). By default Windows OS reserves upper 2GB to itself (for kernel32.dll user32.dll and other things), so what is left for application is lower 2GB. Although there is a switch to change it to 1 vs 3GB. That said you need to remember that in those 2GB or 3GB left for application there is exe loaded, bunch of dll files loaded. So at the end it is pretty fragmented chunk of memory. You probably won't be able to allocate anything larger than 500MB or so. So you need to design your memory system to allocate memory in multiple chunks which will not be next to each other. That is what malloc is doing. It allocates only what you ask (more or less), and not 2GB at startup. And that is what Casey is doing in asset system - loading into independently allocate chunks. On 64-bit you could simply reserve memory in one call for whole asset file and then just commit whatever you want to load.