c++pointersnullundefined-behaviorunspecified-behavior

Is dereferencing a NULL pointer considered unspecified or undefined behaviour?


The consensus of stackoverflow questions say that it is undefined behaviour.

However, I recently saw a 2016 talk by Charles Bay titled:
Instruction Reordering Everywhere: The C++ 'As-If" Rule and the Role of Sequence.

At 37:53 he shows the following:

C++ Terms

Undefined Behaviour: Lack of Constraints
(order of globals initialization)

Unspecified Behaviour: Constraint Violation
(dereferencing NULL pointer)

Now I have conflicting information.
Was this a typo? Has anything changed?


Solution

  • The examples are associated with the wrong things. Regardless of what version of the C++ standard you assume (i.e. nothing has changed within the standards, in this regard).

    Dereferencing a NULL pointer gives undefined behaviour. The standard does not define any constraint on what happens as a result.

    The order of globals initialisation is an example of unspecified behaviour (the standard guarantees that all globals will be initialised [that's a constraint on how globals are initialised] but the order is not specified).