Handmade Hero»Forums»Code
1 posts
Reason for not checking null after memory allocations?
Edited by MundeRaG on Reason: Initial post
Hello,

I recently started following handmade hero and am finding it really helpful and invaluable as a view into an excellent programmer's workflow and decision flow. One thing I noticed in the beginning is that there is no check for null after VirtualAlloc calls. However, I do not know or remember the rationale for it. Can anyone please enlighten me?
498 posts
Reason for not checking null after memory allocations?
HMH's memory strategy is structured around a few big allocations of reasonable size up front. If that fails you won't be able to do anything anyway.

Simon Anciaux
1108 posts
Reason for not checking null after memory allocations?
As ratchetfreak said, if VirtualAlloc fails you can't do anything. You could print a message to the user and do a proper shutdown, but that's not necessary during development where I prefer to crash or assert to find the issue. Better user facing message would be something you do before shipping the game.

If VirtualAlloc failed and the arena allocator doesn't check for that (which I don't know if it does) it might return allocation that starts at 0, and the 0 to 0xffff address range on Windows will trigger an exception if you try to access it (which is a good thing during development).

Another point is that it's "hard" to make VirtualAlloc not return memory. I believe you'd have to request memory at an address already mapped in your process, or request enough memory to fill up available RAM + available swap file (and the swap file might grow on the fly) and even then it might only take into account the memory pages you actually used.