I 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 of the test () function gets corrupted when I use the gcc -O1 flag. Here are some examples:
$ 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 my_stack declaration. Would you classify this as a compiler error, or did I skip something about std :: stack and return std :: pair values?
source share