Bug 57917 - -Wuninitialized
Summary: -Wuninitialized
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.4
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-07-17 08:37 UTC by Nishant Sharma
Modified: 2013-07-17 11:01 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nishant Sharma 2013-07-17 08:37:03 UTC
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.
Comment 1 Jonathan Wakely 2013-07-17 09:24:07 UTC
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 ***
Comment 2 Nishant Sharma 2013-07-17 10:05:26 UTC
(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?
Comment 3 Nishant Sharma 2013-07-17 10:07:49 UTC
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?
Comment 4 Manuel López-Ibáñez 2013-07-17 10:53:39 UTC
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.
Comment 5 Jonathan Wakely 2013-07-17 11:01:55 UTC
Your code is equivalent to:

  int i;  // uninitialized
  i = 1;  // now it's initialized

This should not give a warning.