The constant 0 is used as the null pointer in C and C++. But as in the question “Pointer to a specific fixed address“ there seems to be some possible use of assigning fixed addresses. Is there ever any conceivable need, in any system, for whatever low level task, for accessing the address 0?
If there is, how is that solved with 0 being the null pointer and all?
If not, what makes it certain that there is not such a need?
Neither in C nor in C++ null-pointer value is in any way tied to physical address
0. The fact that you use constant0in the source code to set a pointer to null-pointer value is nothing more than just a piece of syntactic sugar. The compiler is required to translate it into the actual physical address used as null-pointer value on the specific platform.In other words,
0in the source code has no physical importance whatsoever. It could have been42or13, for example. I.e. the language authors, if they so pleased, could have made it so that you’d have to dop = 42in order to set the pointerpto null-pointer value. Again, this does not mean that the physical address42would have to be reserved for null pointers. The compiler would be required to translate source codep = 42into machine code that would stuff the actual physical null-pointer value (0x0000or0xBAAD) into the pointerp. That’s exactly how it is now with constant0.Also note, that neither C nor C++ provides a strictly defined feature that would allow you to assign a specific physical address to a pointer. So your question about “how one would assign 0 address to a pointer” formally has no answer. You simply can’t assign a specific address to a pointer in C/C++. However, in the realm of implementation-defined features, the explicit integer-to-pointer conversion is intended to have that effect. So, you’d do it as follows
Note, that this is not the same as doing
The latter always produces the null-pointer value, while the former in general case does not. The former will normally produce a pointer to physical address
0, which might or might not be the null-pointer value on the given platform.