Uninitialized variables checking?
In Visual Studio, under C/C++ -> Code Generation -> Basic Runtime Checks, it has settings to do basic
runtime checking for uninitialized variables.
http://msdn.microsoft.com/en-us/library/8wtf2dfz.aspx
This seems to report local uninitialized variables in a function, but it doesn't seem to find
uninitialized variables from a class. Is that true? How can I find all uninitialized variables?
Re: Uninitialized variables checking?
Quote:
Originally Posted by
Syslock
In Visual Studio, under C/C++ -> Code Generation -> Basic Runtime Checks, it has settings to do basic
runtime checking for uninitialized variables.
http://msdn.microsoft.com/en-us/library/8wtf2dfz.aspx
This seems to report local uninitialized variables in a function, but it doesn't seem to find
uninitialized variables from a class. Is that true? How can I find all uninitialized variables?
I don't know if the runtime can find these variables, but there really is no need to "find" these variables. You see them right there in your code when you look at the class or struct.
Add a default constructor to your classes that initializes (not assigns) all your member variables.
Code:
class foo
{
int x;
int y;
double z;
public:
foo() : x(0), y(0), z(0.0) {}
};
Note that the above is not assignment -- it is initialization and is not the same thing as assignment. Assignment would have you write code inside the constructor, assigning each member using "=".
Regards,
Paul McKenzie
Re: Uninitialized variables checking?
Thanks, I understand, but I was hoping for a way to do it programmically, as I have to go through a couple hundred source files
by myself.
Re: Uninitialized variables checking?
Get in the habit of initializing whenever you add a variable to a class or create a local variable.
Of course, it won't help you now, but will in the future.
Re: Uninitialized variables checking?
Any dynamic code analysis tool will identify places that any variable is used uninitialized.
I can't speak to the quality of any of the tools for Windows, however.
Re: Uninitialized variables checking?
Quote:
Originally Posted by
Syslock
Thanks, I understand, but I was hoping for a way to do it programmically, as I have to go through a couple hundred source files
by myself.
What you want to do with some sort of tool will take just marginally less time than if you just bit the bullet and went through the files yourself by hand.
You have an idea of what files to look at, and know exactly what makes a struct/class member uninitialized (or I'm assuming you know) -- a tool to show you what you already know tends to add very little to speeding up the process. It isn't like searching for memory leaks or array boundary errors.
Or did you expect this tool to change the source files for you? That would be unreasonable, since the value that variables should be initialized to is up to the coder and the context -- a tool can't do that for you.
Regards,
Paul McKenzie
Re: Uninitialized variables checking?
Quote:
Originally Posted by
Paul McKenzie
Or did you expect this tool to change the source files for you? That would be unreasonable, since the value that variables should be initialized to is up to the coder and the context -- a tool can't do that for you.
A tool that can make changes to the source code wouldn't be unreasonable, as it could just set everthing to zero.
Re: Uninitialized variables checking?
Quote:
Originally Posted by
Syslock
A tool that can make changes to the source code wouldn't be unreasonable, as it could just set everthing to zero.
And what if one of those variables needs to be set to 1 or some other value on initialization? What about non-numeric members? You still have to go through every struct or class that has changed to make sure that the initialization makes sense to your application.
The closest thing that possibly does what you want is a lint utility, i.e. PC-Lint or some other lint checker that goes through your source files and checks for uninitialized members. I doubt any of them changes your source code.
Regards,
Paul McKenzie