C++ standard: dereferencing NULL pointer to get a reference? [duplicate]

Dereferencing a NULL pointer is undefined behavior.

In fact the standard calls this exact situation out in a note (8.3.2/4 “References”):

Note: in particular, a null reference cannot exist in a well-defined program, because the only
way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.


As an aside: The one time I’m aware of that a NULL pointer can be “dereferenced” in a well-defined way is as the operand to the sizeof operator, because the operand to sizeof isn’t actually evaluated (so the dereference never actually occurs).

Leave a Comment