2

This program

int main()
{
    int a, b;
    a = b;
    return 0;
}

compiles without warnings or errors if you compile it with

g++ -Wall test.cpp

However, if you compile it with optimisations on, even at the same warning level

g++ -Wall -Os test.cpp

Then it will start complaining that you're using uninitialised variables!
(Note: the actual program is slightly longer and doesn't expose the problem as clearly as this little example here.)

I've tried to search with google, but either I'm not using the right search terms, or this is not a known phenomenon. Anyway, my questions basically are

  • Why would the compiler give this warning with -Os but not with -O0, even at the same warning level? (If anything, I'd expect it to be the other way around for this little example, since the variables can be optimised away entirely, and then the problem would vanish.)
  • Are there any other compiler options that trigger unexpected warnings like this? Since I'm interested in making my programs bug-free, I like to see all of them!
  • Is this a known feature? If so, where can I find it? I did try searching.
  • Or is this just a glitch in my particular version of the compiler (gcc 4.3.2-1.1, Linux, 32 bit)?
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • I *do* get the warnings using your first compilation line on a variety of recent GCC versions, see answer below. – juanchopanza May 20 '12 at 07:44
  • Do you have a setting somewhere that defaults the O level to 2 or higher? What happens if you enter -O0 manually? Otherwise, maybe my gcc version is just outdated. But this is the version that comes with my repository... I know, I am overdue for a new system. – Mr Lister May 20 '12 at 07:55
  • I explicitly used `-O0`. – juanchopanza May 20 '12 at 08:00

2 Answers2

3

The compiler needs to do extra checks in order to perform some optimizations, which leads it to emit extra warnings. There is a brief explanation of it in the relevant chapter of An Introduction to GCC.

By the way, on my platform (32 bit ubuntu 12.04 on x86) the code produces warnings with gcc 4.6.3, gcc 4.7.0 and with a gcc 4.8 snapshot:

uninitialized.cpp: In function 'int main()': uninitialized.cpp:3:9: warning: variable 'a' set but not used [-Wunused-but-set-variable] uninitialized.cpp:4:10: warning: 'b' is used uninitialized in this function [-Wuninitialized]

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks. That page at least does say "It only works when the program is compiled with optimization, so that data-flow analysis is enabled." So I know it's not just me. But it doesn't explain why. Why can't the compiler do data flow analysis without optimisation? – Mr Lister May 20 '12 at 07:45
  • @MrLister I guess it is an optimiztation. Why spend time doing something if it isn't needed? On the other hand, one would expect `-Wall` to cover this. Unfortunately i cannot reproduce the problem in order to figure out the combination of flags necessary for the warning to kick in. – juanchopanza May 20 '12 at 07:50
  • I disagree with the "if it isn't needed" part. It _is_ needed! In the program above, it's just as bad a bug, with the same bad results, whether you compile with optimisations on or not. – Mr Lister May 20 '12 at 07:57
  • @MrLister In this case, I agree, but the definition of what should be a warning and what an error on GCC is rather capricious, unfortunately. And `Wall` is a misnomer if there ever was one. – juanchopanza May 20 '12 at 08:25
  • Can't disagree with that. And you did answer most of my questions, and it seems that newer versions of the compiler work the way I want, so I can't really ask for more. – Mr Lister May 21 '12 at 21:18
1

With higher optimization levels compiler notices that you are equating non initialized variables. And so the warnings.

Jay D
  • 3,263
  • 4
  • 32
  • 48
  • Are you sure that's it? I mean, -Wall does include -Wuninitialized and -Wmaybe-uninitialized. Maybe I should read up those. – Mr Lister May 20 '12 at 07:38
  • @mrlister : I agree with you !-Wall switches on -Wmaybe-uninitialized and -wuninitialized. But I was talking about the warning at equating two uninitialized variables. – Jay D May 20 '12 at 07:45
  • But apparently, that's what happens. See juanchopanza's answer, which explains more. But not the why. – Mr Lister May 20 '12 at 07:51