未定义的,不确定的,并且实现定义的行为 [英] Undefined, unspecified and implementation-defined behavior

查看:201
本文介绍了未定义的,不确定的,并且实现定义的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C和C ++不确定的,不确定的,和实现定义的行为之间的区别?

What is the difference between undefined, unspecified, and implementation-defined behavior in C and C++?

推荐答案

未定义行为是C和C ++语言的那些方面,可奇怪的程序员1从其他语言(其它语言试图更好地隐藏)。基本上,它是可以编写不以predictable行为方式,即使在节目许多C ++编译器不会报告任何错误的C ++程序!

Undefined behavior is one of those aspects of the C and C++ language that can be surprising to programmers coming from other languages (other languages try to hide it better). Basically, it is possible to write C++ programs that do not behave in a predictable way, even though many C++ compilers will not report any errors in the program!

让我们来看看一个典型的例子:

Let's look at a classic example:

#include <iostream>

int main()
{
    char* p = "hello!\n";   // yes I know, deprecated conversion
    p[0] = 'y';
    p[5] = 'w';
    std::cout << p;
}

变量 P 指向字符串你好!\\ n,以下试两个任务修改字符串。这是什么节目呢?根据第2.14.5 C ++标准的第11段中,它调用的未定义行为的:

The variable p points to the string literal "hello!\n", and the two assignments below try to modify that string literal. What does this program do? According to section 2.14.5 paragraph 11 of the C++ standard, it invokes undefined behavior:

试图修改字符串的效果是不确定的。

The effect of attempting to modify a string literal is undefined.

我可以听到人们的尖叫声别急,我可以编译这个没有问题,得到输出或你是什么意思不确定,字符串存储在只读存储器,因此第一个任务尝试导致核心转储。这正是与不确定的行为问题。基本上,标准允许你一旦调用未定义行为(甚至鼻魔)发生的任何事情。如果根据语言的你的心理模型一个正确的行为,该模型是完全错误的; C ++标准拥有国内唯一的票,期限。

I can hear people screaming "But wait, I can compile this no problem and get the output yellow" or "What do you mean undefined, string literals are stored in read-only memory, so the first assignment attempt results in a core dump". This is exactly the problem with undefined behavior. Basically, the standard allows anything to happen once you invoke undefined behavior (even nasal demons). If there is a "correct" behavior according to your mental model of the language, that model is simply wrong; The C++ standard has the only vote, period.

未定义行为的其他例子包括访问超越其边界的数组,取消引用空指针,的accessing对象后他们的一生结束或写据说聪明的前pressions 我+ + + + I

Other examples of undefined behavior include accessing an array beyond its bounds, dereferencing the null pointer, accessing objects after their lifetime ended or writing allegedly clever expressions like i++ + ++i.

C ++标准的1.9节也提到未定义行为的少了两个危险兄弟的未指定的行为的和的实现定义的:

Section 1.9 of the C++ standard also mentions undefined behavior's two less dangerous brothers, unspecified behavior and implementation-defined behavior:

在本国际标准中的语义描述定义参数不确定性的抽象机。

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine.

抽象机的某些方面和操作本国际标准中被描述为实现定义(例如,的sizeof(INT)) 。这些构成了抽象机的参数。每个实施应包括文档,描述在这些方面的特征和行为。

Certain aspects and operations of the abstract machine are described in this International Standard as implementation-defined (for example, sizeof(int)). These constitute the parameters of the abstract machine. Each implementation shall include documentation describing its characteristics and behavior in these respects.

其他某些方面和抽象机的操作本国际标准中被描述为未指定(例如,为了参数评价一个函数)。如有可能,该国际标准定义了一组允许的行为。这些定义抽象机的非确定性的方面。

Certain other aspects and operations of the abstract machine are described in this International Standard as unspecified (for example, order of evaluation of arguments to a function). Where possible, this International Standard defines a set of allowable behaviors. These define the nondeterministic aspects of the abstract machine.

某些其他操作均在本国际标准中描述为未定义(例如,解引用空指针的作用)。 [注意本标准规定了的,包含未定义行为的程序的行为没有任何要求 - 注完的。]

Certain other operations are described in this International Standard as undefined (for example, the effect of dereferencing the null pointer). [ Note: this International Standard imposes no requirements on the behavior of programs that contain undefined behavior.end note ]

具体而言,部分1.3.24规定:

Specifically, section 1.3.24 states:

允许未定义行为的范围从与联合国predictable结果完全无视的情况下,在环境的记录方式的特点翻译或程序执行过程中的行为(有或没有发行的诊断消息),以终止翻译或执行(并出具诊断消息)。

Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

你能做些什么,以避免陷入不确定的行为?基本上,你必须阅读用谁知道他们在说些什么作家的良好的C ++书籍。螺杆网络教程。螺杆bullschildt。

What can you do to avoid running into undefined behavior? Basically, you have to read good C++ books by authors who know what they're talking about. Screw internet tutorials. Screw bullschildt.

这篇关于未定义的,不确定的,并且实现定义的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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