variable default values

This is a discussion on variable default values within the C++ Programming forums, part of the General Programming Boards category; WTF I must say, I'm just learning C++ and I have to declare my variables with a value, ie int ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    variable default values

    WTF I must say, I'm just learning C++ and I have to declare my variables with a value, ie

    int x;
    x = 0;

    or

    int x = 0;

    or I get some funky number when doing my projects.... lost about 20 minutes figuring this out. Could someone tell me why C++ does not default to 0? or is it a bug in my compiler?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Because it only creates a variable and hold whatever junk was where that variable now is. Why would it default to 0 ? It's not even wish able to always default to 0. Why would you even use a variable without assigning it a value in the first place ? That's why your app crashed...

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    No it's not a compiler bug, it is normal behaviour.
    Built-in types don't get initialised by default, probably for speed reasons. Quite often the value will be set in subsequent code before it is used, and the compiler might have trouble figuring out that it doesn't need to initialise to zero. Hence the language is designed so that you have to be explicit.
    Code:
    //use:
    int x = 0;
    //or
    int x(0);
    Desolation: Some languages do default global variables (at least) to zero, so his question is quite understandable.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I'm guessing that the reason static and global variables get initialized to 0 by default, and automatic variables don't, is that the former only need to be initialized once in the life of the program, while the latter could need the initialization an arbitrarily large number of times, so doing it by default could be very wasteful. Is this correct?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I think the reason is that initializing globals to 0 is free, because the program loaders of most OSs do it anyway.
    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

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you compile with the highest warning level (which you should be doing anyways) you will get a warning if you try to use a variable that hasn't been initialized.

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I never compile with the highest warning level. Is this a bad practice?

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by l2u View Post
    I never compile with the highest warning level. Is this a bad practice?
    yes.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    most compilers will try to place uninitialized global variables in the BSS segment.

    In an object module compiled from C, the bss section contains the local variables (but not functions) that were declared with the static keyword, except for those with non-zero initial values. (In C, static variables are initialized to zero by default.) It also contains the non-local (both extern and static) variables that are also initialized to zero (either explicitly or by default).
    http://en.wikipedia.org/wiki/Block_Started_by_Symbol

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, even C++ should initialize GLOBAL (or static local) variables to zero (either by setting them to zero in DATA section or just leaving it to the loader to get it done when initializing the BSS section). Otherwise, old C code can't be compiled in a C++ environment.

    Local variables, however, are not defined to have any particular value - whatever happens to be the value at the location of that variable. As stated above, this is for performance reasons, since a value of zero is not necessarily the value wanted by the programmer, setting all local variables to zero, only for them to be set to something else later, is pretty pointless.

    [Debug mode of MSVC will actually set local variables to a decidedly non-zero value that is almost certain to be an invalid pointer and too large an index (I beleive it fills local storage with 0xCC bytes). This is so that it's easier to detect if the variable ISN'T set to a value]

    Also, gcc will only detect unset variables at certain level of optimization, because it's part of "flow analyzis" (to say if a variable is set or not, you obviously need to analyze the flow of the code, unless it's a very trivial case - which is usually not the ones that need help), which is not run at the no optimization level.

    --
    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.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by sl34k View Post
    most compilers will try to place uninitialized global variables in the BSS segment.
    It's not uninitialized -- it's zero. There's no such thing as an uninitialized global variable.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Thanks for all the information. A friend pointed out that this information is in the beginning of the book we are studying. But since I read the first chapter a year ago (mostly pointless fluff) I didn't re-read it again. Oops. big mistake.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    It's not uninitialized -- it's zero. There's no such thing as an uninitialized global variable.
    It think that was meant to say "not explicitly initialized global variables".

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Holding multi values with one variable
    By Bitphire in forum C++ Programming
    Replies: 16
    Last Post: 09-27-2004, 11:32 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. Adding Default values to controls in Dialog boxes
    By juhigarg in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2001, 11:44 PM
  5. Default values in function prototypes
    By wdicks in forum C Programming
    Replies: 13
    Last Post: 10-10-2001, 01:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21