Thread: Uninitialized variables

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Uninitialized variables

    Hi everyone.
    I'm new here, and I have a question, very simple for most of you... I guess.
    Here's the code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int a, b, c;
        cout << a << endl << b << endl << c <<endl;
        system ("pause");
        return (0);
    }
    The result is:

    2
    16
    2293672

    How and why got the three variables those values??

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rife
    How and why got the three variables those values??
    They happened to be the contents of the memory that those variables were allocated.
    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

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Thank you.
    I presumed they should get value of 0.

    So, the variables always should be initialiyed if we want to operate with them.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Rife View Post
    Thank you.
    I presumed they should get value of 0.

    So, the variables always should be initialiyed if we want to operate with them.
    Yes, all local variables must be initialized. Local variables have whatever they happens to be in the memory they are given in the function [unless it's a debug build from V7.0+ of MS Visual studio, in which case uninitialized variables have a specific bit-pattern (e.g. 0xCC) filled into them to make it easier to detect uninitialized variables - but this is also not zero].

    GLOBAL (and static) variables are initialized to zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    As Matsp noted, in certain situations your variables will be initialized to 0 but it's considered very bad form and poor practice to expect your compiler to do the work you should be doing.

    The long and short of it -- always initialize your variables.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Depends on what you consider the "work you should be doing". I personally like passing as much work as possible to the compiler.
    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
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    In this case it's initializing variables/pointers/whatever. I generally take the approach "If I'm not initializing it myself expect bugs."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM