g++ compiler: optimization flag adds warning message

c++, g++, optimization, warnings

Solution

That's expected. The optimizations cause a specific code analysis to run and that's how gcc finds the un-initialized variables. It's in the manual page:

. . . these warnings depend on optimization

http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Problem

I noticed this interesting behaviour of the g++ compiler, if I add a -O3 flag to the compiler, I get ``` otsu.cpp:220: warning: ‘x’ may be used uninitialized in this function ``` However, when I do not use optimization and instead use a debug flag -g I got no warnings at all. Now, I trust the compiler more when the -g flag is on; however, I'm wondering if this is well defined behaviour that should be expected? For clarity, the code that causes this is something along these lines: ``` int x; //uninitialized getAValueForX( &x ); // function makes use of x, // but x is unitialized ``` where ``` void getAValueForX( int *x ) { *x = 4; } ``` or something along those lines, obviously more complex.

Original source