取消引用 NULL 指针是否被视为未指定或未定义的行为? [英] Is dereferencing a NULL pointer considered unspecified or undefined behaviour?

查看:60
本文介绍了取消引用 NULL 指针是否被视为未指定或未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stackoverflow 问题的共识说这是未定义的行为.

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

然而,我最近看到了 Charles Bay 2016 年的演讲,题为:
指令重新排序:C++As-If"规则和序列的作用.

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

37:53,他展示了以下内容:

At 37:53 he shows the following:

C++ 术语

未定义行为:缺乏约束
(全局初始化顺序)

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

未指明行为:违反约束
(取消引用 NULL指针)

Unspecified Behaviour: Constraint Violation
(dereferencing NULL pointer)

现在我有相互矛盾的信息.
这是打字错误吗?有什么变化吗?

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

推荐答案

这些示例与错误的事情相关联.无论您假设是哪个版本的 C++ 标准(即,在这方面,标准中没有任何变化).

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

取消引用 NULL 指针会产生未定义的行为.该标准没有对结果发生的情况定义任何约束.

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

这篇关于取消引用 NULL 指针是否被视为未指定或未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆