Thread: how do I know a variable is not initialized?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    how do I know a variable is not initialized?

    dear there,

    I need to print all initialized variables, and some the uninitialized variables got printed, which mess up the output. how do I detect it before send it to printf or cout? thanks.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could always just write

    int foo = 0;

    and then the problem goes away. This type of declaration is called initialization.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A rule of thumb is to declare variables near first use, where you are able to initialise them appropriately.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by patiobarbecue View Post
    dear there,

    I need to print all initialized variables, and some the uninitialized variables got printed, which mess up the output. how do I detect it before send it to printf or cout? thanks.
    If a variable is uninitialized, and this does not cause any problems, then by definition that variable is unnecessary. Just get rid of it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not always, since sometimes you assign some value to it before use (not necessarily initialization).
    Good compilers and debuggers will also warn or break if you use an uninitialized variable (Visual Studio [IDE + compiler + debugger] does it for sure, GCC [compiler] highly likely as well, not about others).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    GCC only emits the warning when you compile with optimizations and warnings on, because the flow analyzer necessary for this warning isn't run without optimizations.

    As a programmer, you cannot and should not detect uninitialized variables at runtime, the reason being that looking at the value of the variable invokes undefined behavior. You're supposed to statically prove that a variable cannot be uninitialized by the time it's used. Which isn't all that hard, really.

    (And by the way of terminology, "uninitialized" in this context means "never having received a (initial) value. Yes, Elysia, another case where common usage conflicts with C++-specific terminology. But in this case, the C++-specific terminology doesn't actually exist: "not having an initializer" isn't called "uninitialized", it's called "not having an initializer".)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Eh, how am I supposed to remember all the terms? Suffice to say that the message got through, I hope.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    27
    As whiteflags said, you can (and should) set the variable to 0 immediately upon declaration. However, if 0 is a potential value for your variables, an alternative is to set it to some other value (like -1) that will be a signal to the system that the variable is unitialized.

    Code:
    short iArray[5];
    //initalizing array
    for(char x=0;x<5;x++)
        {
        iArray[x]=0;
        }
    
    //output values that are initialized
    //zero means the variables are NOT initialized
    for(char x=0;x<5;x++)
        {
        if(iArray[x])
            {
            cout<<x<<": "<<iArray[x]<<endl;
            }
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM