未指定和未定义的行为需要在同一个程序与同一个环境中的同一个编译器的编译之间是一致的吗? [英] Are unspecified and undefined behavior required to be consistent between compiles of the same program with the same compiler in the same environment?

查看:123
本文介绍了未指定和未定义的行为需要在同一个程序与同一个环境中的同一个编译器的编译之间是一致的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我的程序包含一个特定的结构C ++标准状态为未指定的行为。这基本上意味着实现必须做一些合理的事情,但允许不记录它。但是,每次编译具有未指定行为的特定结构时,或者允许在不同的编译中产生不同的行为时,产生相同行为所需的实现是什么?

Let's pretend my program contains a specific construct the C++ Standard states to be unspecified behavior. This basically means the implementation has to do something reasonable but is allowed not to document it. But is the implementation required to produce the same behavior every time it compiles a specific construct with unspecified behavior or is it allowed to produce different behavior in different compiles?

行为?让我们假设我的程序包含一个根据标准的UB构造。允许实现展现任何行为。但是这种行为可以在同一个编译器的同一个程序的编译器与同一个环境中的相同设置之间有所不同吗?换句话说,如果我在文件X.cpp中的第78行取消引用一个空指针,并且在这种情况下,驱动器的实现格式是否意味着它会在程序重新编译后执行相同的操作?

What about undefined behavior? Let's pretend my program contains a construct that is UB according to the Standard. The implementation is allowed to exhibit any behavior. But can this behavior differ between compiles of the same program on the same compiler with same settings in the same environment? In other words, if I dereference a null pointer on line 78 in file X.cpp and the implementation formats the drive in such case does it mean that it will do the same after the program is recompiled?

问题是...我使用相同的编译器在相同的编译器设置下编译同一个程序。将构造声明为未指定的行为和未定义的行为在每次编译时产生每个相同的行为,或者允许它们在编译之间有所不同。

The question is... I compile the same program with the same compier in the same environment with the same compiler settings. Will construct stated to be unspecified behavior and undefined behavior produce each the same behavior on each compile or are they allowed to differ between compiles?

推荐答案

p>未定义的行为可以在同一程序的运行之间变化,甚至在程序的相同运行中执行相同的代码之间变化。例如,未初始化(自动)变量的值是未定义的,然后其实际值就是发生在内存中该位置的任何值。显然这可能会有所不同。

Undefined behavior can vary between runs of the same program, and even between execution of the same code in the same run of the program. As an example, the value of an uninitialized (automatic) variable is undefined, and then its actual value is just whatever value that happened to be at that place in memory. Obviously, this can vary.

编辑:

太。例如,函数参数的求值顺序是未指定的,因此如果它们有副作用,那些副作用可能以任何顺序发生。这可能打印Hi!Ho!或Ho!Hi!:

This goes for unspecified behavior too. For example, the order of evaluation of function arguments is unspecified, so if they have side effects, those side effects can occur in any order. This may print "Hi!Ho!" or "Ho!Hi!":

f( printf("Hi!"), printf("Ho!") );

这也可以在执行之间变化。正如标准所说:
抽象机器的一个实例因此可以具有给定程序和给定输入的多于一个可能的执行序列。区别在于,使用未定义的行为,可能会发生以下情况:计算机可能爆炸,重新格式化磁盘或任何内容。如果行为未指定,则计算机不允许爆炸。

This can vary between executions, too. As the standard says: "An instance of the abstract machine can thus have more than one possible execution sequence for a given program and a given input." The difference is that with undefined behavior, anything can happen: the computer can explode, reformat the disk, or whatever. If the behavior is unspecified, the computer is not allowed to explode.

还有实现定义的行为,例如 sizeof(int)。对于同一个编译器,它必须始终相同。

There is also implementation-defined behavior, such as the value of sizeof(int). This must be the same at all times, for the same compiler.

这篇关于未指定和未定义的行为需要在同一个程序与同一个环境中的同一个编译器的编译之间是一致的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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