• Advertisement
Sign in to follow this  

Force VS warnings for uninit member variables?

This topic is 3320 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know of a way to force Visual Studio to give warnings, or even errors when member variables are not initialized, (even better if it could know they weren't initialized in a constructor)?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by _fastcall
There is the "Treat errors as warnings (/WX)" option under Configuration Properties->C/C++->General in the project properties...


Yes, but I'm not even getting errors. The problem is that I am working with a huge code base and I need to find all cases where people have forgotten to initialize member variables in their constructors or otherwise.

Share this post


Link to post
Share on other sites
Quote:
Original post by lordikon
Does anyone know of a way to force Visual Studio to give warnings, or even errors when member variables are not initialized, (even better if it could know they weren't initialized in a constructor)?
It probably can't be done (At least not unless your compiler is particularly smart), because the variable could be written to from outside the compilation unit that the class is defined in (And a class doesn't even have to be defined in one compilation unit).

Without the compiler analyzing every single bit of your code that uses the variable, and building a dependency graph between them, I can't see it being possible.
However, if you initialise every variable in the constructor, and/or use smart pointers and other ways to make sure your variables are initialised, it shouldn't be that big a problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by lordikon
Does anyone know of a way to force Visual Studio to give warnings, or even errors when member variables are not initialized, (even better if it could know they weren't initialized in a constructor)?
It probably can't be done (At least not unless your compiler is particularly smart), because the variable could be written to from outside the compilation unit that the class is defined in (And a class doesn't even have to be defined in one compilation unit).

Without the compiler analyzing every single bit of your code that uses the variable, and building a dependency graph between them, I can't see it being possible.
However, if you initialise every variable in the constructor, and/or use smart pointers and other ways to make sure your variables are initialised, it shouldn't be that big a problem.


Initializing them all within the constructor is the thing we want to be doing, however, people do forget sometimes. With such a large code base it would take me days to go through and check each class.

I've seen software that will do this, but it is around $350 per PC. I was hoping VS could do it somehow.

Share this post


Link to post
Share on other sites
Static analysis of C++ code is really hard so most compilers only do the most basic amount possible.

I believe the software your referring to is probably PC Lint, which is pretty much the industry standard for this kind of thing. I don't know of free alternatives for C++ (there's Splint for C but I never researched it all that much). There's a reason why PC Lint is so expensive [smile]

Share this post


Link to post
Share on other sites
Problem is, uninitialized variable in C++ is not an error, nor is accessing it.

The most trivial, common and sane code:
struct Foo {
int x;
int y;
};

...

Foo foo;
foo.x = 10;
if (special) y = 20;
It may not be best practice, but the whole reason for using C++ is to be able to do something like this.

This would require final, readonly or similar functionality, something which C++ doesn't have. But such languages also define strict initialization order, as well as default values.

Quote:
I believe the software your referring to is probably PC Lint, which is pretty much the industry standard for this kind of thing. I don't know of free alternatives for C++ (there's Splint for C but I never researched it all that much). There's a reason why PC Lint is so expensive


Such software is ok if you have the time (and/or budget, since time is money) and reasonably managed team and development process. Otherwise trying to enforce such tools will literally kill anyone's ability to write code due to excessive amount of warnings.

I found it typical that the best non-trivial 100+ lines of code I wrote produced dozens of warnings, while passing gcc and MVC strictest checks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Such software is ok if you have the time (and/or budget, since time is money) and reasonably managed team and development process. Otherwise trying to enforce such tools will literally kill anyone's ability to write code due to excessive amount of warnings.

I found it typical that the best non-trivial 100+ lines of code I wrote produced dozens of warnings, while passing gcc and MVC strictest checks.


I agree. It'd be crazy to introduce PC-Lint to a project that is already underway. You really want to use it right from the start because otherwise it's very noisy!

Share this post


Link to post
Share on other sites
Not initializing member variables is not always a problem. In some cases you don't want to initialize member variables because it can cause performance problems. For instance a standard vector class will generally have a no-init constructor:


class Vector
{
Vector() {}
Vector( float x, float y, float z ) : mX(x), mY(y), mZ(z) {}

float mX;
float mY;
float mZ;
};




Because often with vectors the first thing you do with them is assign them to a different vector, or pass them as an argument to a function, or assign them to the return value of a function, etc etc. Having all constructors always initialize the variables can cause un-necessary writes in very performance problematic sections of code.

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
Not initializing member variables is not always a problem. In some cases you don't want to initialize member variables because it can cause performance problems. For instance a standard vector class will generally have a no-init constructor:

*** Source Snippet Removed ***


Are you sure that won't actually zero-initialize the data members? :)

Share this post


Link to post
Share on other sites
That won't. Though this would:

Vector() : mX(), mY(), mZ() {}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement