Handmade Hero»Forums»Code
26 posts
Is the highest memory address (0xffffffffffffffff) reserved by the OS?
Edited by Opoiregwfetags on

I've realized that often in my code (and I've seen it in HmH too) I assume that adding 1 to a valid memory position won't do an overflow.

For example, to iterate over things contained in a memory block, you can do:

int *limit = (int *)(my_memory_block + memory_block_size);
int *it = (int *)my_memory_block;
while(it < limit){
    ...
    it++;
}

But if your allocator had given you a block including the last addressable byte, then this would not work as expected, because 'limit' would overflow into 0. Is this a real concern?


EDIT: I found online that on Windows and Linux the upper and lower addresses are used by the OS so your application won't get that. I guess all other 32-bit or 64-bit platforms will do the same, so I guess it's not really a concern, but if someone knows more about other platforms, that can still help clear things out.

Simon Anciaux
1109 posts
Is the highest memory address (0xffffffffffffffff) reserved by the OS?

This video might interest you, but it's only about windows: Mysteries of Memory Management Revealed,with Mark Russinovich.

Mārtiņš Možeiko
2238 posts / 1 project
Is the highest memory address (0xffffffffffffffff) reserved by the OS?

And not all 64-bit addresses are valid on x86 platform. Currently only 48-bit address space is available to use - 128TB for userspace, and 128TB for kernel space. So for your user space pointers upper 17 bits are always zero.

26 posts
Is the highest memory address (0xffffffffffffffff) reserved by the OS?

Thanks as always guys. The video linked seems very interesting.