Why do operators after returning change the return value?

C ++ returns an invalid value in the following code:

#include <iostream> #include <vector> using namespace std; int f(){ vector< int * > v[2]; return 1; v[1].push_back(NULL); } int main(){ cout << f(); } 

Conclusion:

 205960 

When I return the commnet line after the return, it works fine:

 #include <iostream> #include <vector> using namespace std; int f(){ vector< int * > v[2]; return 1; //v[1].push_back(NULL); } int main(){ cout << f(); } 

Conclusion:

 1 

I am using code :: blocks with mingw32-g ++. exe compiler. Mingw version: gcc version 4.4.1 (TDM-2 mingw32).

+6
source share
1 answer

Your compiler has an error. Fortunately, it is also out of date. You should upgrade - g ++ to version 4.6.2, which also implements most of C ++ 11, which is very useful.

If you decide to stick with the old compiler, it is also a decision to accept its flaws.

Edit: If you are really stuck with 4.4 (e.g. due to PHB), this series is still supported. You can upgrade to GCC 4.4.6, released only in April this year.

+10
source

Source: https://habr.com/ru/post/902557/


All Articles