Null Pointer Dereferencing
Debates on dereferencing null pointers in C/C++ causing undefined behavior, compiler optimizations assuming no null derefs, and the representation of NULL pointers across platforms.
Activity Over Time
Top Contributors
Keywords
Sample Comments
You are not allowed to dereference a null pointer no matter how you use it even if you later convert it back to address.
Dereferencing a null pointer is Undefined Behavior.
Comparing a pointer to null isn't UB. De-referencing invalid pointers is ub.
If you never dereference a null pointer, then there will be no UB of that type.
Dereferencing a null pointer (i.e. use) is undefined behavior.
No compiler I'm aware of implements non-zero null pointers on systems where address 0 is valid (e.g. armv7), so it ends up being kind of a moot point.
* No NULL pointer can be dereferenced.NULL pointers CAN be dereferenced, it all depends on the environment you run on.
In standard C NULL pointers cannot be dereferenced. Full stop, there is nothing to argue about it.There are environments that either lack memory protection and do not allow invalid pointer derereferences to be caught, which means that you can't rely on the MMU to catch mistakes. In this case either deal with silent errors or get a compiler that is able (at a cost) to sanitize any pointer access.There are other systems in which memory address 0 is a perfectly valid address. The ABI of
Are you sure the compiler didn't say "since having a null pointer gives undefined behavior, we can optimize out the part that confirms the address is non-null"?
If the pointer is null and you go ahead and dereference it anyway then yes, that's dumb, and the compiler is free to remove the null check.