难道"未定义行为"真正使* *事情要发生? [英] Does "Undefined Behavior" really permit *anything* to happen?

查看:101
本文介绍了难道"未定义行为"真正使* *事情要发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这个问题不好旨在为有关的不确定的行为(德)值得讨论的论坛,但是这是那种什么成了。在任何情况下,这个线程关于一个假设C编译器使用没有未定义的行为可能是额外的利息那些谁认为这是一个重要的话题。

This question was not intended as a forum for discussion about the (de)merits of undefined behavior, but that's sort of what it became. In any case, this thread about a hypothetical C-compiler with no undefined behavior may be of additional interest to those who think this is an important topic.

的未定义行为的经典例子杜撰的,当然,鼻魔—物理不可能的,无论什么样的C和C ++标准允许。

The classic apocryphal example of "undefined behavior" is, of course, "nasal demons" — a physical impossibility, regardless of what the C and C++ standards permit.

由于C和C ++社区往往把这样一个注重未定义行为未predictability和观念,编译器被允许导致程序字面上做什么的时候未定义遇到的行为,我曾以为,该标准提出不限制任何上的,好了,不确定的行为的行为。

Because the C and C++ communities tend to put such an emphasis on the unpredictability of undefined behavior and the idea that the compiler is allowed to cause the program to do literally anything when undefined behavior is encountered, I had assumed that the standard puts no restrictions whatsoever on the behavior of, well, undefined behavior.

但是,<一个href=\"https://github.com/cplusplus/draft/blob/dd95ad99c2d548f40ddabdb47a59c0c77056c072/source/intro.tex\"相对=nofollow>有关在C ++标准的报价似乎的:

[C ++ 14:defns.undefined]: [..] 的允许未定义行为从联合国$完全无视的情况范围p $ pdictable结果,在环境的记录方式特性翻译或程序执行期间的行为(有或没有发出一个诊断消息),以终止翻译或执行(与发行的诊断消息)。 [..]

[C++14: defns.undefined]: [..] 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). [..]

这实际上是指定一小部分可能的选项:

This actually specifies a small set of possible options:


  • 忽略的情况 - 是的,标准接着说,这将有非predictable结果,但这是不一样的,因为编译器的插入 code(我假设将是一个prerequisite,你知道,鼻魔)。

  • 行为的环境的记录方式的特点 - 这实际上听起来比较良性的。 (当然,我没有听说过鼻恶魔的任何记录的情况。)

  • 终止翻译或执行 - 一个诊断,也不能少。将所有UB会表现得如此漂亮。

  • Ignoring the situation -- Yes, the standard goes on to say that this will have "unpredictable results", but that's not the same as the compiler inserting code (which I assume would be a prerequisite for, you know, nasal demons).
  • Behaving in a documented manner characteristic of the environment -- this actually sounds relatively benign. (I certainly haven't heard of any documented cases of nasal demons.)
  • Terminating translation or execution -- with a diagnostic, no less. Would that all UB would behave so nicely.

我认为在大多数情况下,编译器选择忽略未定义的行为;例如,读取未初始化的内存时,它会presumably进行反优化插入任何code,以确保一致的行为。我想,陌生人类型的未定义行为(如时间旅行)将属于第二类下 - 但这需要这样的行为被记录和?环境特征(所以我想鼻魔只能由地狱电脑生产)

I assume that in most cases, compilers choose to ignore the undefined behavior; for example, when reading uninitialized memory, it would presumably be an anti-optimization to insert any code to ensure consistent behavior. I suppose that the stranger types of undefined behavior (such as "time travel") would fall under the second category--but this requires that such behaviors be documented and "characteristic of the environment" (so I guess nasal demons are only produced by infernal computers?).

我误解的定义是什么?难道这些打算作为的什么可能构成不确定的行为,而不是选择一个COM prehensive列表单纯的的例子吗?是声称一切皆有可能仅仅意味着作为无视形势的一个意想不到的副作用?

Am I misunderstanding the definition? Are these intended as mere examples of what could constitute undefined behavior, rather than a comprehensive list of options? Is the claim that "anything can happen" meant merely as an unexpected side-effect of ignoring the situation?

编辑:澄清两个小点:


  • 我认为这是从原来的问题清楚,我想对大多数人来说是的,但无论如何,我会拼出来。我也知道魔鬼鼻是舌头在脸颊

  • 请不要写的(其他)答案解释说,UB允许特定于平台的编译器优化,除非你的的解释它是如何允许为优化实现定义的行为的不会的允许。

  • I thought it was clear from the original question, and I think to most people it was, but I'll spell it out anyway: I do realize that "nasal demons" is tongue-in-cheek.
  • Please do not write an(other) answer explaining that UB allows for platform-specific compiler optimizations, unless you also explain how it allows for optimizations that implementation-defined behavior wouldn't allow.

推荐答案

是的,它允许发生什么事。该说明只是提供例子。该定义是pretty明确的:

Yes, it permits anything to happen. The note is just giving examples. The definition is pretty clear:

未定义行为:行为这本国际标准中并没有规定要求

Undefined behavior: behavior for which this International Standard imposes no requirements.

这篇关于难道&QUOT;未定义行为&QUOT;真正使* *事情要发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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