For C++ development for 32-bit systems (be it Linux, Mac OS or
Windows, PowerPC
or x86) I have initialised pointers that
would otherwise be undefined (e.g. they can not immediately
get a proper value) like so:
int *pInt = reinterpret_cast<int *>(0xDEADBEEF);
(To save typing and being DRY the right-hand side would normally be in a constant, e.g. BAD_PTR.)
If pInt is dereferenced before it gets a proper value then it will crash immediately on most systems (instead of crashing much later when some memory is overwritten or going into a very long loop).
Of course the behavior is dependent on the underlying hardware (getting a 4 byte integer from the odd address 0xDEADBEEF from a user process may be perfectly valid), but the crashing has been 100% reliable for all the systems I have developed for so far (Mac OS 68xxx, Mac OS PowerPC, Linux Redhat Pentium, Windows GUI Pentium, Windows console Pentium). For instance on PowerPC it is illegal (bus fault) to fetch a 4 byte integer from an odd address.
What is a good value for this on 64-bit systems?
0
can't be mapped. In Linux, it's easy to map to address0
by changing an option in the kernel. In any case, the lesson learned here is not to use stupid patterns to mark pointers as invalid, usenull
or a separate flag in the struct. Assuming it will just crash is completely irresponsible and ignorant, if you're lucky, you will only get a segfault, it's likely that this can lead to remote code execution, and already has many, many, many, times in the past. – Outbuilding(void*)0
to point somewhere other than address 0. However, the interrupt vector table isn't a C object or C function, so I don't think compilers are obligated to guarantee that NULL doesn't point to the interrupt vector table. – Reversioner