Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a valid pointer be allocated at address 0?

On any of the 'major platforms' (which I'm defining as Windows, Mac and Linux for the purpose of this question,) is it conceivable for a validly allocated pointer to be allocated at address 0 in a programs address space (therefore messing up comparisons to NULL?) Does the standard even allow a compiler/platform to have a valid allocation at address 0?

like image 667
0x5f3759df Avatar asked Jan 20 '23 06:01

0x5f3759df


1 Answers

The C++ standard allows it, but such a pointer will not compare equal to literal 0 (the NULL pointer constant).

User-mode applications in the major OSes, however, will never have a valid pointer at 0, or even in the range -65536 to 65535 (to help detect offsets from a NULL pointer).

For the most part, 0 as a usable address only exists in embedded systems, and very rarely in kernels of PC operating systems. But virtual memory systems reserve it, without any exceptions I've ever heard of.

like image 122
Ben Voigt Avatar answered Jan 30 '23 14:01

Ben Voigt