Missing warning about uninitialized variable
Ian Lance Taylor
iant@google.com
Fri Nov 27 14:29:00 GMT 2009
David Sveningsson <ext@sidvind.com> writes:
> Hi, I recently ran into an issue with an uninitialized pointer which I
> expected g++ to warn about.
>
> class Foo {
> public:
> Foo* a(){ return this; }
> };
>
> int main(int argc, const char*[] ){
> for ( int i = 0; i < 6; i++ ){
> Foo* foo = foo->a();
> }
> }
>
> This code compiles without any warnings (with -Wall) with both
> g++-4.4.2 and g++-4.2.4. Removing the for-loop gives me a warning as
> expected:
>
> foo.cpp: In function ‘int main(int, const char**)’:
> foo.cpp:8: warning: ‘foo’ is used uninitialized in this function
>
> I know this case is a bit silly but it happened because of a typo and
> went unnoticed for a while.
>
> Is my reasoning flawed or should g++ emit a warning?
You may get a warning with -Winit-self.
However, this sounds like a bug either way. Please consider filing a
bug report as described at http://gcc.gnu.org/bugs/ .
Ian
More information about the Gcc-help
mailing list