std::stack 破坏返回值

发布于 2024-09-29 18:57:18 字数 1256 浏览 1 评论 0原文

我已将代码简化为以下内容以说明我的问题:

#include <iostream>
#include <stack>
#include <utility>

std::pair<double,double> test(double a, double b)
{
    std::stack<int> my_stack;
    return std::make_pair<double,double>(a,b);
}

int main()
{
    std::pair<double,double> p = test(1.1,2.2);
    std::cout << p.first << " " << p.second << "\n";

    return 0;
}

当我使用 gcc -O1 标志时,test() 函数的返回值被损坏。以下是一些示例输出:

$ gcc -O2 a.cxx  -lstdc++
$ ./a.out
1.1 2.2
$ gcc -O1 a.cxx  -lstdc++
$ ./a.out
2.60831e-317 2.60657e-317
$ gcc -v
Reading specs from /usr/lib64/gcc-lib/x86_64-suse-linux/3.3.3/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-    prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --enable-languages=c,c++,f77,objc,java,ada --disable-checking --libdir=/usr/lib64 --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit x86_64-suse-linux Thread model: posix
gcc version 3.3.3 (SuSE Linux)

此代码适用于除“-O1”之外的所有 gcc 优化标志。如果我删除 my_stack 的声明,它也会起作用。您是否会将其归类为编译器错误,或者我是否缺少有关 std::stack 和返回 std::pair 值的信息?

I've reduced my code to the following to illustrate my problem:

#include <iostream>
#include <stack>
#include <utility>

std::pair<double,double> test(double a, double b)
{
    std::stack<int> my_stack;
    return std::make_pair<double,double>(a,b);
}

int main()
{
    std::pair<double,double> p = test(1.1,2.2);
    std::cout << p.first << " " << p.second << "\n";

    return 0;
}

The return value from the test() function gets corrupted when I use the gcc -O1 flag. Here is some sample output:

$ gcc -O2 a.cxx  -lstdc++
$ ./a.out
1.1 2.2
$ gcc -O1 a.cxx  -lstdc++
$ ./a.out
2.60831e-317 2.60657e-317
$ gcc -v
Reading specs from /usr/lib64/gcc-lib/x86_64-suse-linux/3.3.3/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-    prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --enable-languages=c,c++,f77,objc,java,ada --disable-checking --libdir=/usr/lib64 --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit x86_64-suse-linux Thread model: posix
gcc version 3.3.3 (SuSE Linux)

This code works with all gcc optimazation flags except "-O1". It also works if I remove the declaration of my_stack. Would you classify this as a compiler bug, or am I missing something about std::stack and returning std::pair values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

少女情怀诗 2024-10-06 18:57:18

绝对是编译器错误。
顺便说一句,它没有用我的 GCC 版本(4.4.5)重现。

SuSE 9.1 附带的 GCC 3.3.3 版本已知已损坏(请参阅此处 )。

Definitely a compiler bug.
BTW it is not reproduced with my GCC version (4.4.5).

The version of GCC 3.3.3 shipped with SuSE 9.1 is known to be broken (see here).

公布 2024-10-06 18:57:18

这是一个错误。检查该bug是否已报告,如果没有,请报告。

It's a bug. Check if the bug has been reported, if not, report it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文