Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::stack corrupts return value

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?

like image 286
Doug Masterson Avatar asked Nov 01 '10 18:11

Doug Masterson


1 Answers

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

like image 116
vitaut Avatar answered Oct 05 '22 13:10

vitaut