1

Is it possible to add additional warning flags to g++ so that it will warn me about the unitialized b variable in the following code?

#include <iostream>
using namespace std;
int main() {
  int a, b;
  cin >> a;
  while (a>0) b++;
  cout << a;
}

Edit: I forgot to mention that I have tried turning on the flags listed at this other question: Flags to enable thorough and verbose g++ warnings but nothing triggered. ("Tickled!" as I have learned below.)

Community
  • 1
  • 1
daveagp
  • 2,599
  • 2
  • 20
  • 19

2 Answers2

7

The option you are after is likely -Wmaybe-uninitialized or -Wuninitialized. Both of these are part of the -Wall option which turns on these, and many other warnings (the -Wxxx options are related to warnings).

Full documentation of the warning options for gcc may be read at: Options to Request or Suppress Warnings in the gcc documentation.

You may also find that -Wextra may be of use to you (either alone, or in conjunction with -Wall). -Wextra also enables -Wuninitialized, but it has other items that are not always set by -Wall and are ones that I like to see (things like -Wunused-parameter and -Wunused-but-set-parameter)... though thats not specific to this bit of code.


That said... (yea, there's a "that said")... I can't seem to tickle an error with this functionality with gcc that is is available on http://gcc.godbolt.org.

Extending your code with some that is specifically described in the documentation for -Wmaybe-uninitialized

#include <iostream>
using namespace std;
int main() {
  int a, b;
  int x, y, z;
  // warning: variable ‘y’ set but not used [-Wunused-but-set-variable]
  cin >> a;
  while (a>0) b++;
  switch(a) {
    case 1: x = 1; y = 1; z++; break;
    // warning: ‘z’ may be used uninitialized in this function [-Wmaybe-uninitialized]
    case 2: x = 4; y = 2; break;
    case 3: x = 5; y = 3;
  }
  cout << a;
  cout << b;
  cout << x;
}

This is an attempt to tickle a number of the unused and unitized warnings. I was able to get the z variable to produce the error, but for some reason b++ in that while loop does not generate an error or warning with gcc (tested using gcc 4.9.0).

Warnings with gcc 4.9.0

clang version 3.4.1, however, does produce warnings with these command line options for both b and z

Warnings with clang 3.4.1

And while -Wall and -Wextra should produce the warnings you are after, for some reason they do not produce the desired warnings for this specific piece of code in gcc 4.9.0

  • @chris `#clang++ -std=c++1y` ... its not gcc. –  Sep 21 '14 at 17:45
  • `#` is a comment. Look two lines below. It's just easy to keep a few things I use often and switch between them without having to form them again. – chris Sep 21 '14 at 17:46
  • Hmm... that is indeed the case. http://gcc.godbolt.org (another nice online compiler) can show other various errors, but this one is slipping through. –  Sep 21 '14 at 17:55
  • @chris so, you are indeed correct. I stills stand by my answer that for the general case, those options *should* produce the desired warnings and certain other bits of code do generate the error. This specific SSCCE that daveagp provides doesn't appear to tickle those warnings though with gcc (though I can make *other* bits of code that are similar in nature do it). I would be quite happy to find out what isn't working right (is it a misunderstanding of the standard on my part? a bug in gcc?). –  Sep 21 '14 at 18:18
  • 1
    Oh, of course. I was surprised to find no warnings, too. – chris Sep 21 '14 at 18:19
0

Yes!!! You must use -Wuninitialized.

See https://gcc.gnu.org/wiki/Better_Uninitialized_Warnings

echo
  • 104
  • 4