NaN 在不同的 g++ 中处理不同版本

发布于 2024-11-18 23:31:12 字数 966 浏览 2 评论 0原文

考虑以下程序,该程序显然有错误:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神爱温柔 2024-11-25 23:31:12

您显示的代码具有未定义的行为,无法从所有控制路径返回 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.

倾城°AllureLove 2024-11-25 23:31:12

您的函数返回 double 但当 n % 2 != 0 时它没有返回任何内容。

你正处于行为未定义的境地。

Your function returns double yet when n % 2 != 0 it isn't returning anything.

You are in the land of undefined behavior.

无声无音无过去 2024-11-25 23:31:12

我想知道为什么是前者
gcc版本似乎花了一些功夫
并生成一致的代码
返回 NaN 并且当此行为
更改为观察到的
后者的 gcc 版本。另外,通过
“可预测”我的意思不是如何写
C++ 中的好程序,但是如何
控制这个 gcc 行为(也许用
一些命令行选项?)。

因为您处于未定义行为的领域,所以不太可能有任何原因。编译器编写者(理所当然地)倾向于不花任何精力来研究编译器在未定义行为的情况下所做的事情。无论这里发生什么,几乎可以肯定都是编译器版本之间(显然)不相关的更改引起的紧急行为,而不是任何编译器工程师的故意选择。不会有一个标志来改变它。不要依赖它。修复你的代码。

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?).

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.

清旖 2024-11-25 23:31:12

忽略警告将带来危险。

if (n % 2 == 0) return 0.0; else return 0.0;

工作正常,否则,在一半的情况下 test() 返回垃圾。

未定义的行为与版本无关,因为我也得到 NaN:

$ gcc --version
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
$ uname -a
Linux tallguy 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux

Ignore the warnings at your peril.

if (n % 2 == 0) return 0.0; else return 0.0;

Works properly, otherwise, in half the cases test() returns garbage.

And undefined behavior is version independent as I get NaNs also:

$ gcc --version
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
$ uname -a
Linux tallguy 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文