views:

787

answers:

8

I am looking for easy way to find uninitialized class member variable.

Runtime or compile time both methods OK.

Currently breakpoint in class constuctor and watch variable one by one.

A: 

You can check them in code editor when they are declare or in constructor. Why you have to do that at runtime.

affan
+3  A: 

-Wuninitialized ?

(This only checks if a variable is used uninitialized, i.e. if

struct Q { 
  int x, y;
  Q() : x(2) {}
  int get_xy() const { return x*y; }
};

g++ will warn only when the user calls get_xy() without assigning to y.)

KennyTM
Also requires -O1 or above, which isn't the default.
Roger Pate
I am unable to get g++ 4.3.3 to warn for data members with -Wuninitialized, are you sure it works here? (Simple test: add `int main() { return Q().get_xy(); }` to your code.)
Roger Pate
@Roger-plate: Unfortunately you need to use `int main() { Q q; return q.get_xy(); }` to work.
KennyTM
@Kenny: Thanks. That is unfortunate.
Roger Pate
A: 

Your compiler might provide warnings for this. Make sure those warnings are enabled and then check the build log.

Failing that there are third party tools such as lint that will check your source code for errors like this.

PhilMY
+5  A: 

Valgrind can tell you if you are on linux.

Zitrax
+2  A: 

Valgrind (FREE, on Linux) and Purify (on Windows) find un-initialized variables, invalid pointers and such by running your code in a special virtual machine.

This is easy to use and extremely powerful; it will likely find many bugs beyond the obvious un-initialized variables.

Coverity, Klocwork and Lint can find un-initialized variables using static code analysis.

Will
A: 

If you're using Visual Studio, you could compile in debug mode, stop the program in the debugger and look for which variables are initialised to bytes containing 0xCC (stack) or 0xCD (heap).

Though personally, I'd invest in a static analysis tool for a more thorough approach.

Kaz Dragon
+1  A: 

/analyze on Visual Studio ("Team System")

Terry Mahaffey
+1  A: 

If you use GCC you can use the -Weffc++ flag, which generates a warnings when a variable isn't initialized in the member initialisation list. This:

class Foo
{
  int v;
  Foo() {}
};

Leads to:

$ g++ -c -Weffc++ foo.cpp -o foo.o
foo.cpp: In constructor ‘Foo::Foo()’:
foo.cpp:4: warning: ‘Foo::v’ should be initialized in the member initialization list

One downside is that -Weffc++ will also warn you when a variable has a proper default constructor and initialisation thus wouldn't be necessary. It will also warn you when you initialize a variable in the constructor, but not in the member initialisation list. And it warns on many other C++ style issues, such as missing copy-constructors, so you might need to clean up your code a bit when you want to use -Weffc++ on a regular basis.

There is also a bug that causes it to always give you a warning when using anonymous unions, which you currently can't work around other then switching off the warning, which can be done with:

#pragma GCC diagnostic ignored "-Weffc++"

Overall however I have found -Weffc++ to be incredible useful in catching lots of common C++ mistakes.

Grumbel