什么是C ++不确定的行为?它是如何从不确定的行为有什么不同? [英] What is indeterminate behavior in C++ ? How is it different from undefined behavior?

查看:191
本文介绍了什么是C ++不确定的行为?它是如何从不确定的行为有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是多么的不确定行为和未定义在C ++中的行为有什么区别?这是分类有效期为C codeS也?

What is the difference between an indeterminate behaviour and an undefined behaviour in C++? Is this classification valid for C codes also?

推荐答案

编辑1: C11和最后的草案C ++ 11都可以在这里在线:的 C11草案N1570 并的 C ++ 11 n3242草案,如果你不具备的最终标准和精彩的副本的他们的样子。 (文字外观和一些措辞/语法编辑其他调整已经完成。)

EDIT 1: The last drafts of C11 and C++11 are available online here: C11 draft N1570 and C++11 draft n3242 if you don't have a copy of the final standards and wonderful what they look like. (Other adjustments to text appearance and some wording/grammar edits have been done.)

编辑2:固定的行为所有出现的是行为,以符合标准

EDIT 2: Fixed all occurrences of "behaviour" to be "behavior" to match the standard.

搜索的C ++ 11和C11标准没有对比赛的不确定的规则未定义规则的。有同类项的不确定的值 indeterminately测序不确定的初始化的等。

Searching the C++11 and C11 standards there are no matches for indeterminate rule or undefined rule. There are terms like indeterminate value, indeterminately sequenced, indeterminate uninitialized, etc.

如果陷阱和异常的说法似乎诺曼格雷的答案怪异,知道这些条款确实反映了栏目的相关定义3 C11标准

If talk of traps and exceptions seems weird in Norman Gray's answer, know that those terms do reflect the relevant definitions in Section 3 in the C11 standard.

C ++依赖基于C的定义。关于类型的许多有用的定义的行为的可以在C11的第3(在C11)找到。例如,不确定的值的以3.19.2定义。请留意,C11的第2节(规范性引用)提供了其他来源获得额外的术语间pretation和第4节定义在这样的情况下的未定义行为的出现作为的不符合的结果的使用标准。

C++ relies on C's definitions. Many useful definitions concerning types of behaviour can be found in C11's Section 3 (in C11). For example, indeterminate value is defined in 3.19.2. Do take note that C11's Section 2 (Normative References) provides other sources for additional terminology interpretation and Section 4 defines when cases such as undefined behavior occur as a result of not complying with the standard.

C11的3.4节定义的行为的,3.4.1定义的实现定义的,3.4.2定义的区域设置特定的行为的3.4。 3定义的未定义行为的,3.4.4定义的未指定的行为的。对于的的(第3.19节),还有的实现定义的值不确定的值的和的未经指定的值

C11's section 3.4 defines behavior, 3.4.1 defines implementation-defined behavior, 3.4.2 defines locale-specific behavior, 3.4.3 defines undefined behavior, 3.4.4 defines unspecified behavior. For value (Section 3.19), there are implementation-defined value, indeterminate value, and unspecified value.

严格地说,这个术语的不确定指的未指定/未知状态的通过本身并不会导致的未定义行为的。例如,这个C ++ code涉及一个不确定的值:{INT X = X; }。 (这实际上是在C ++ 11标准的例子。)在这里,x被定义为第一是整数,但是在这一点上不具有良好定义的值--then它被初始化为任何(不确定/未知)看重它了!

Loosely speaking, the term indeterminate refers to an unspecified/unknown state that by itself doesn't result in undefined behavior. For example, this C++ code involves an indeterminate value: { int x = x; }. (This is actually an example in the C++11 standard.) Here x is defined to be an integer first but at that point it does not have a well-defined value --then it is initialized to whatever (indeterminate/unknown) value it has!

众所周知的长期的未定义行为在3.4.3定义在C11,指的任何情况下

The well-known term undefined behavior is defined in 3.4.3 in C11 and refers to any situation of a

不可移植的或错误的程序构造或错误的数据,这本国际标准并没有规定要求

nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

在换句话说未定义行为的一些错误(逻辑或状态),无论接下来发生的是未知的!因此,人们可以作出的未定义[行为]规则,指出:编写C / C ++ code时,为了避免不确定的行为! : - )

In other words undefined behavior is some error (in logic or state) and whatever happens next is unknown! So one could make an undefined [behavior] rule that states: avoid undefined behavior when writing C/C++ code! :-)

不确定[行为]规则将状态:除非它是需要避免写不确定code的的它不影响程序正确性或可移植性。所以不像未定义的行为,不确定的行为的不一定的暗示code /数据是错误的,但是,随后的使用可能是也可能不是错误 - 因此护理必须确保程序的正确性得以维持。

An indeterminate [behavior] rule would be to state: avoid writing indeterminate code unless it is needed and it does not affect program correctness or portability. So unlike undefined behavior, indeterminate behavior does not necessarily imply that code/data is erroneous, however, its subsequent use may or may not be erroneous --so care is required to ensure program correctness is maintained.

其他条款一样的 indeterminately测序的是在身体文本(例如,C11 5.1.2.3第3段; C ++ 11,第1.9节第13段;也就是说,在[intro.executation]) 。 (您可能已经猜到,它指的操作步骤的一个未指定的顺序。)

Other terms like indeterminately sequenced are in the body text (e.g., C11 5.1.2.3 para 3; C++11, section 1.9 para. 13; i.e., in [intro.executation]). (As you might guess, it refers an unspecified order of operational steps.)

如果IMO人感兴趣所有这些细微之处,都获得了C ++ 11和C11的标准是必须的。这将允许一个探索到所需级别的细节如果你没有这样的本文提供的链接有助于探索这种与上次公布的草案C11和C ++ 11标准与定义等需要

IMO if one is interested in all of these nuances, acquiring both the C++11 and C11 standards is a must. This will permit one to explore to the desired level-of-detail needed with definitions, etc. If you don't have such the links provided herein will help you explore such with the last published draft C11 and C++11 standards.

这篇关于什么是C ++不确定的行为?它是如何从不确定的行为有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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