Thread: Undefined variables have values???

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    220

    Undefined variables have values???

    Ok..I'm really confused on this one....why is the output of my program with the source:
    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
        int i;
        do
        {
            std::cout << i;
        }while(getch());
    return 0;
    }
    output:
    4370436

    ????

    I didn't define i with a value.......why is this happening and what does it mean?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Because i actually refers to some location in memory. If it is not zeroed out first or initialized, it will contain spurious data or the data that is currently at the address of 'i'.

    The compiler will not initialize variables to zero or any value for that matter - failure to init variables in code can produce errors and in some cases crashes.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >The compiler will not initialize variables to zero or any value for that matter
    With the exception of global variables, I believe.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Soooo...basically what your saying is..my compiler is bailing out by defining 'i' with a value of 'i''s memory address?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    your compiler isn't bailing out on anything, nor is it giving you the value of the memory address.

    When you declare a variable, the compiler goes out and sets some memory blocks to hold the data. When you set it equal to something, it will set those memory blocks equal to the value that you set it to. If you never set it to anything, it can only use whatever was already in those memory blocks.

    illustration:
    Code:
      int i;       // i's memory block might look like this -> [334567]     
      int j;       // j's memory block might look like this -> [552001]
    
      int i = 23; //i's memory block now looks like this -> [000023]
    
      cout << i;   //output:   23
      cout << j;   //output:   552001
    hopefully this will clarify what the others and me are trying to say. Of course, those values are made up, but you should get the idea.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    o.o

    If the memory block has no value....why is the result that value?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Tronic
    o.o

    If the memory block has no value....why is the result that value?
    It does have a value. Just some random one. Maybe the program you ran earlier in the day used that address and that value is left over in there. It's just some garbage value.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Oh, I see Thank you.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Just to reinforce this, and the difference between local and global variables, run the following:

    Code:
    #include <iostream>
    using namespace std;
    
    int globalInt;
    double globalDouble;
    
    int main()
    {
        int localInt;
        double localDouble;
    
        cout << "Local int: " << localInt << endl;
        cout << "Global int: " << globalInt << endl;
        cout << "Local double: " << localDouble << endl;
        cout << "Global double: " << globalDouble << endl;
    }
    Gambling. The new yoga, with cash prizes!

  10. #10
    Board Conservative UnregdRegd's Avatar
    Join Date
    Jul 2003
    Posts
    154
    You might want to get into the practice of initializing all variables to some meaningful default value, like 0. And for reference, Java and probably C# automatically initialize all variables to some default value if you don't. Even when you're using those languages, you don't want to get into the habit of relying on the compiler to do your work for you.
    I am a programmer. My first duty is to God, then to nation, then to employer, then to family, then to friends, then to computer, and finally to myself. I code with dignity, honor, and integrity.

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Quote Originally Posted by UnregdRegd
    You might want to get into the practice of initializing all variables to some meaningful default value, like 0.
    If you're using C++, a better practice than initialising to a default value is to defer declaring your variables until you need them, then initialise at the point of declaration.
    Gambling. The new yoga, with cash prizes!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM