CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2001
    Posts
    251

    Arrow 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?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Uninitialized variables checking?

    Quote Originally Posted by Syslock View Post
    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

  3. #3
    Join Date
    Nov 2001
    Posts
    251

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Uninitialized variables checking?

    Quote Originally Posted by Syslock View Post
    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

  7. #7
    Join Date
    Nov 2001
    Posts
    251

    Re: Uninitialized variables checking?

    Quote Originally Posted by Paul McKenzie View Post
    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.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Uninitialized variables checking?

    Quote Originally Posted by Syslock View Post
    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
    Last edited by Paul McKenzie; May 26th, 2010 at 05:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured