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?

Community
  • 1
  • 1
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • 1
    It is ass-backwards. Fail! – Kaz Oct 08 '16 at 05:45
  • 2
    The examples should be swapped. It is most likely a typo! – Nawaz Oct 08 '16 at 05: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. – Xeo Oct 08 '16 at 05:51
  • @Xeo: Meanwhile, DR#315 (http://www.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. – AnT stands with Russia Oct 08 '16 at 06:09

2 Answers2

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 ]

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    Not there... it is somewhere else the spec has mentioned that *dereferencing* nullptr is UB. – Nawaz Oct 08 '16 at 05:51
  • @Nawaz, I found the undefined behavior bit inside a *Note*. – R Sahu Oct 08 '16 at 05:58
  • Well, it means the first quote is irrelevant here (if so, it should be removed from this answer). – Nawaz Oct 08 '16 at 06:00
  • @Nawaz, true. I find it rather strange that such an important aspect of the language will be left to a *Note*. – R Sahu Oct 08 '16 at 06:01
  • @Nawaz, I'll be grateful if you can point me to other such sections of the standard. – R Sahu Oct 08 '16 at 06:04
  • 1
    I just asked a question about this matter http://stackoverflow.com/questions/39860732/the-apparent-underspecification-of-one-past-the-end-subscripting-for-both-raw-a and it looks woefully underspecified to me. DR#315 (http://www.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 (http://www.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. – AnT stands with Russia Oct 08 '16 at 06:05
  • @AnT, thanks for the links. It was educational for me to go through your question. – R Sahu Oct 08 '16 at 06:15
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).

Peter
  • 35,646
  • 4
  • 32
  • 74