Is dereferencing a NULL pointer considered unspecified or undefined behaviour?
Asked Answered
E

2

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?

Er answered 8/10, 2016 at 5:41 Comment(4)
It is ass-backwards. Fail!Tiros
The examples should be swapped. It is most likely a typo!Layard
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.Jemappes
@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.Photodrama
S
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).

Systematology answered 8/10, 2016 at 7:39 Comment(0)
P
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 ]

Psychophysiology answered 8/10, 2016 at 5:49 Comment(7)
Not there... it is somewhere else the spec has mentioned that dereferencing nullptr is UB.Layard
@Nawaz, I found the undefined behavior bit inside a Note.Psychophysiology
Well, it means the first quote is irrelevant here (if so, it should be removed from this answer).Layard
@Nawaz, true. I find it rather strange that such an important aspect of the language will be left to a Note.Psychophysiology
@Nawaz, I'll be grateful if you can point me to other such sections of the standard.Psychophysiology
I just asked a question about this matter #39861232 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.Photodrama
@AnT, thanks for the links. It was educational for me to go through your question.Psychophysiology
S
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).

Systematology answered 8/10, 2016 at 7:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.