I\'m writing C code for a system where address 0x0000 is valid and contains port I/O. Therefore, any possible bugs that access a NULL pointer will remain undetected and at t
The C standard does not require null pointers to be at the machine's address zero. HOWEVER, casting a 0
constant to a pointer value must result in a NULL
pointer (§6.3.2.3/3), and evaluating the null pointer as a boolean must be false. This can be a bit awkward if you really do want a zero address, and NULL
is not the zero address.
Nevertheless, with (heavy) modifications to the compiler and standard library, it's not impossible to have NULL
be represented with an alternate bit pattern while still remaining strictly conformant to the standard library. It is not sufficient to simply change the definition of NULL
itself however, as then NULL
would evaluate to true.
Specifically, you would need to:
-1
.0
to check for the magic value instead (§6.5.9/6)There are some things you do not have to handle. For example:
int x = 0;
void *p = (void*)x;
After this, p
is NOT guaranteed to be a null pointer. Only constant assignments need be handled (this is a good approach for accessing true address zero). Likewise:
int x = 0;
assert(x == (void*)0); // CAN BE FALSE
Also:
void *p = NULL;
int x = (int)p;
x
is not guaranteed to be 0
.
In short, this very condition was apparently considered by the C language committee, and considerations made for those who would choose an alternate representation for NULL. All you have to do now is make major changes to your compiler, and hey presto you're done :)
As a side note, it may be possible to implement these changes with a source code transformation stage before the compiler proper. That is, instead of the normal flow of preprocessor -> compiler -> assembler -> linker, you'd add a preprocessor -> NULL transformation -> compiler -> assembler -> linker. Then you could do transformations like:
p = 0;
if (p) { ... }
/* becomes */
p = (void*)-1;
if ((void*)(p) != (void*)(-1)) { ... }
This would require a full C parser, as well as a type parser and analysis of typedefs and variable declarations to determine which identifiers correspond to pointers. However, by doing this you could avoid having to make changes to the code generation portions of the compiler proper. clang may be useful for implementing this - I understand it was designed with transformations like this in mind. You would still likely need to make changes to the standard library as well of course.