std::stack 破坏返回值
我已将代码简化为以下内容以说明我的问题:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绝对是编译器错误。
顺便说一句,它没有用我的 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).
这是一个错误。检查该bug是否已报告,如果没有,请报告。
It's a bug. Check if the bug has been reported, if not, report it.