NaN 在不同的 g++ 中处理不同版本
考虑以下程序,该程序显然有错误:
#include <cstdio>
double test(int n) {
if (n % 2 == 0)
return 0.0;
// warning: control reaches end of non-void function
}
int main() {
printf("%.9lf\n", test(0));
printf("%.9lf\n", test(1));
printf("%.9lf\n", test(2));
printf("%.9lf\n", test(3));
return 0;
}
当在 32 位 Ubuntu 8.04 上使用 g++ 版本 4.2.4 (Ubuntu 4.2.4-1ubuntu4) 编译时,它会产生以下输出:
0.000000000
nan
0.000000000
nan
当使用 g++ 版本 4.4.3 编译时(Ubuntu 4.4.3-4ubuntu5) 在 64 位 Ubuntu 10.04 上,它会产生以下输出:
0.000000000
0.000000000
0.000000000
0.000000000
看来较旧的编译器做了一些额外的工作工作以返回 NaN 而不是垃圾,而较新的编译器只是返回内存中的任何内容。究竟是什么导致了这种行为差异,以及如何控制它并使其在不同编译器版本之间可预测?
编辑:很抱歉没有提前提及未定义的行为。我知道差异来自于该程序具有未定义的行为。我想知道为什么前一个 gcc 版本似乎付出了一些努力并生成了始终返回 NaN 的代码以及何时此行为更改为在后者中观察到的行为海湾合作委员会版本。另外,我所说的“可预测”并不是指如何用 C++ 编写好的程序,而是如何控制 gcc 的行为(也许使用一些命令行选项?)。
Consider the following program, which is obviously buggy:
#include <cstdio>
double test(int n) {
if (n % 2 == 0)
return 0.0;
// warning: control reaches end of non-void function
}
int main() {
printf("%.9lf\n", test(0));
printf("%.9lf\n", test(1));
printf("%.9lf\n", test(2));
printf("%.9lf\n", test(3));
return 0;
}
When compiled with g++, version 4.2.4 (Ubuntu 4.2.4-1ubuntu4) on a 32-bit Ubuntu 8.04, it produces the following output:
0.000000000
nan
0.000000000
nan
When compiled with g++, version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) on a 64-bit Ubuntu 10.04, it produces the following output:
0.000000000
0.000000000
0.000000000
0.000000000
It seems that the older compiler does some extra work in order to return NaN instead of garbage, while the newer compiler simply returns whatever there is in memory. What exactly causes this difference in behavior and how to control it and make it predictable across different compiler versions?
EDIT: Sorry for not mentioning undefined behavior earlier. I know that the difference comes from the fact that this program has undefined behavior. What I'd like to know why the former gcc version seems to put some effort and produce code that consistently returns NaN and when this behavior changed to the one observed in the latter gcc version. Also, by "predictable" I meant not how to write good programs in C++, but how to control this gcc behavior (maybe with some command line options?).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您显示的代码具有未定义的行为,无法从所有控制路径返回
test
中的值。没有什么可以期待的,也没有什么可以控制它的,除非首先编写一个正确的程序。
编辑:我认为您不应该依赖编译器似乎所做的事情。
即使你认为你应该这样做,你也不应该这样做。
查看反汇编将揭示这段特定的代码将被编译成什么,但是您将无法泛化到这段特定的代码之外的任何其他内容,尤其是在存在优化的情况下。未定义的行为真正意味着您认为它会做什么:除非您碰巧掌握了 GCC 的代码库,否则无法保证您所观察到的内容可以在其他上下文中重现。
这里唯一明智的解决方案是使用 -Wall 进行编译并修复那些“并非所有返回路径都返回值”,以便行为 1) 定义,2) 由您定义。
The code you show has undefined behavior, by failing to return a value in
test
from all control paths.There is nothing to expect, and nothing you can do to control it, except by writing a correct program in the first place.
EDIT: I maintain that you shouldn't rely on what the compiler seems to do.
Even if you think you should, you shouldn't.
Looking at the dissasembly will reveal what this particular piece of code will be compiled into, but you will not be able to generalize to anything else than this particular piece of code, especially in the presence of optimizations. Undefined behavior really means what you think it does: unless you happen to master GCC's codebase, there will be no guarantee that what you observe will be reproducible in another context.
The only sensible solution here is to compile with -Wall and fix those "not all return paths return a value", so that the behaviour is 1) defined, 2) defined by you.
您的函数返回
double
但当n % 2 != 0
时它没有返回任何内容。你正处于行为未定义的境地。
Your function returns
double
yet whenn % 2 != 0
it isn't returning anything.You are in the land of undefined behavior.
因为您处于未定义行为的领域,所以不太可能有任何原因。编译器编写者(理所当然地)倾向于不花任何精力来研究编译器在未定义行为的情况下所做的事情。无论这里发生什么,几乎可以肯定都是编译器版本之间(显然)不相关的更改引起的紧急行为,而不是任何编译器工程师的故意选择。不会有一个标志来改变它。不要依赖它。修复你的代码。
Because you're in the realm of undefined behavior, it is highly unlikely that there is any why. Compiler writers (justifiably) tend to not spend any effort on what a compiler does in cases of undefined behavior. Whatever happens here is almost certainly emergent behavior from (apparently) unrelated changes between the compiler versions, not a deliberate choice on the part of any compiler engineer. There is not going to be a flag to change it. Do not depend on it. Fix your code.
忽略警告将带来危险。
工作正常,否则,在一半的情况下
test()
返回垃圾。未定义的行为与版本无关,因为我也得到 NaN:
Ignore the warnings at your peril.
Works properly, otherwise, in half the cases
test()
returns garbage.And undefined behavior is version independent as I get NaNs also: