I work in Windows 7, the environment Code :: Blocks 16.01, the recommended version of their MinGW default version.
There are some very similar code examples:
#include <iostream> void bar() { int a; std::cout << &a << std::endl; return; } void foo() { int a = 3; std::cout << &a << std::endl; return; } int main() { foo(); bar(); return 0; }
The displayed addresses on my machine match 100% of the time, and this is quite understandable behavior.
#include <iostream> void bar() { int a; std::cout << a << std::endl; return; } void foo() { int a = 3; std::cout << a << std::endl; return; } int main() { foo(); bar(); return 0; }
And here the oddities begin. If any optimization is enabled, it displays:
3
0
and if you remove them, it displays:
3
3
In this code example:
#include <iostream> void bar() { int a; std::cout << a << " " << &a << std::endl; return; } void foo() { int a = 3; std::cout << a << " " << &a << std::endl; return; } int main() { foo(); bar(); return 0; }
3 <address>
3 <address>
always displayed, regardless of optimization flags.
Why does the inclusion of optimization flags so selectively affect the auto-nulling of variables?
By the way, when using g ++ from under Ubuntu 14.04, similar results were obtained.