Hi, Please see code snippet below. g++ -v gave me following output: g++ -v Using built-in specs. Target: x86_64-unknown-linux-gnu Configured with: /home/gcc-4.2.4/configure --prefix=/usr/local/soft/gcc/4.2.4 --disable-nls --enable-languages=c,c++ Thread model: posix gcc version 4.2.4 In the code below, class variable isABC is not initialized. We came across a crash in one particular RHEL machine which got fixed once I initialized this variable in the class constructor. In another machine, the software started giving absurd results until I initialized this variable in constructor. I thought of finding an option in gcc which can report uninitialized variables in class. I finally came across -Wuninitialized. I compiled this code using: g++ test.cpp -Wuninitialized -O3 I was not reported any warning in this case. Is this the correct usage? If not, is there any switch that can report such warning? ============================================= class A { private: bool isABC; public: void setABC(bool); }; void A::setABC(bool flag) { isABC = flag; } int main() { A a; return -1; } =========================== But, the following case reports the following warning: test.cpp: In function 'int main()': test.cpp:31: warning: 'a.A::a' is used uninitialized in this function test.cpp:38: note: 'a.A::a' was declared here -------------------------- class A { bool ggg; int a; public: void mA() { printf("haha"); ++a; int g = 2/a; printf("%i\n",g); } }; int main() { A a; a.mA(); return -1; } Thanks. Please help.
GCC 4.2 is ancient and no longer supported. This is not "critical", it's your code that has a bug, not the compiler. You don't use A::isABC in the program, so it's a poor testcase. There are lots of existing PRs about this, e.g. PR 2972 and PR 42000 and PR 19808 *** This bug has been marked as a duplicate of bug 19808 ***
(In reply to Jonathan Wakely from comment #1) > GCC 4.2 is ancient and no longer supported. > > This is not "critical", it's your code that has a bug, not the compiler. > > You don't use A::isABC in the program, so it's a poor testcase. > > There are lots of existing PRs about this, e.g. PR 2972 and PR 42000 and PR > 19808 > > *** This bug has been marked as a duplicate of bug 19808 *** Even if from main, if I execute: a.setABC(false); Then also it does NOT report a warning. This should have worked isn't it? My main intent is to ask how to catch such warnings?
Even if from main, if I execute: a.setABC(false); Then also it does NOT report a warning. This should have worked isn't it? What should be done so that compiler treats this as a warning OR do you think this is not a valid case for being reported & if not, then why would compiler not report anything?
Nishant, this is not a good testcase because A::isABC is not used. Even if you call a.setABC(false), isABC is only assigned but its uninitialized value is not used. In fact, in your testcase, since 'a' is never used before the program finishes, GCC may just delete it and return -1. Nonetheless, there are many testcases for which GCC fails to provide a warning, check those PRs (bug reports) and make sure that your real testcase is not covered by any of them before opening a new PR. You can find a list of -Wuninitialized PRs here: http://gcc.gnu.org/PR24639 You could add yourself to the CC list of those PRs if you are interested in tracking progress, but more testcases are not really needed. What is needed is people willing to work on fixes.
Your code is equivalent to: int i; // uninitialized i = 1; // now it's initialized This should not give a warning.