Is dereferencing a NULL pointer considered unspecified or undefined behaviour?

假如想象 提交于 2021-01-27 20:41:27

问题


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?


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




回答2:


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 ]



来源:https://stackoverflow.com/questions/39929099/is-dereferencing-a-null-pointer-considered-unspecified-or-undefined-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!