On Sun, Dec 7, 2008 at 4:21 AM, Goswin von Brederlow <[EMAIL PROTECTED]> wrote: > Hi, > > After spending the better part of the evening finding a bug caused by > an uninitialized pointer I would love to get a Warning about > uninitialized variables from g++ in cases like Foo, Bar and Baz below: > > [EMAIL PROTECTED]:~% cat foo.cc > class Foo { > Foo() { } > int *p; > }; > > class Bar { > Bar() { *p = 1; } > int *p; > }; > > class Baz { > Baz() { } > int get_p() { return *p; } > int *p; > }; > > [EMAIL PROTECTED]:~% g++ -g -O2 -W -Wall -Werror -Wuninitialized -c foo.cc > > [EMAIL PROTECTED]:~% g++ --version > g++ (Debian 4.3.2-1) 4.3.2 > Copyright (C) 2008 Free Software Foundation, Inc. > This is free software; see the source for copying conditions. There is NO > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. > > > > In Foo the variable is just uninitialized, in Bar it is definetly used > uninitialized and in Baz it might be used uninitialized. Is there any > -W switch to make g++ detect such errors or is that a shortcomming of > -Wuninitialized?
Well, the compiler just sees Bar::Bar() (struct Bar * const this) { int * D.1736; <bb 2>: D.1736_2 = this_1(D)->p; *D.1736_2 ={v} 1; return; } where 'this' is a incoming pointer. It obviously doesn't know that it is supposed to be initialized in this function. That is, -Wuninitialized doesn't know about constructors. Richard.