2

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?

4
  • 1
    It is ass-backwards. Fail! Commented Oct 8, 2016 at 5:45
  • 2
    The examples should be swapped. It is most likely a typo! Commented Oct 8, 2016 at 5:47
  • 1
    As Nawaz says. The standard doesn't specify the total order in which globals / statics are initialised, but it does define that they are initialised (in some unspecified order). A null pointer dereference is still undefined behaviour. Commented Oct 8, 2016 at 5:51
  • @Xeo: Meanwhile, DR#315 (open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#315) claims that dereferencing a null pointer is OK for calling a member function, if there's no lvalue-to-rvalue conversion involved. Commented Oct 8, 2016 at 6:09

2 Answers 2

3

It is undefined behavior.

From 8.3.2 References of the C++11 Standard (emphasis mine):

5 ... [ 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 described in 9.6, a reference cannot be bound directly to a bit-field. —end note ]

Sign up to request clarification or add additional context in comments.

7 Comments

Not there... it is somewhere else the spec has mentioned that dereferencing nullptr is UB.
@Nawaz, I found the undefined behavior bit inside a Note.
Well, it means the first quote is irrelevant here (if so, it should be removed from this answer).
@Nawaz, true. I find it rather strange that such an important aspect of the language will be left to a Note.
I just asked a question about this matter stackoverflow.com/questions/39860732/… and it looks woefully underspecified to me. DR#315 (open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#315) claims that it is OK to derefence a null pointer (marked NAD). However, DR#232 (open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232), which was planning to allow null pointer dereference has been in "drafting" stage since 2000. The corresponding wording in not in the standard.
|
1

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.