Globals initialization

This is a discussion on Globals initialization within the C++ Programming forums, part of the General Programming Boards category; I have a question about the initialization of global variables: If I define in one .cpp-file "int a = 12" ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    Globals initialization

    I have a question about the initialization of global variables:
    If I define in one .cpp-file "int a = 12" and then declare in the .cpp-file with the main function "extern int a", is it guaranteed by c++-standard that "int a" was initialized before entering main()? Or could it happen that "int a" is initialized with 0 at use, because the statics initialization of the other cpp-file wasn't executed?

    The global is always correctly initialized across several files with my compiler (gcc), but I'd like to know if it's defined to happen that way.
    And yes, I know that globals are evil.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    The only guarantee you have is that the global will be initialized before it's first use. However, most implementations will initialize all globals before main() is called.

    Google-Groups("C C++ global initialization")
    Look in comp.lang.c.moderated and comp.lang.c++.moderated.

    gg

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    I think it's strange that people have been already complaining about this in 1993 and it was never changed. That the problem of dependencies among those definitions might be hard to solve is understandable, but at least the initialization of all globals in all files before entering main could be required by the standard.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    New++, the initialization of globals in a single translation unit, ie., a .cpp file with all the defines preprocessed, is defined, the globals defined first are initialized first. (You can always depend on the 0 initialization.) But for globals spanning multiple files, the initialization order is undefined, usually depending on the order of the files given to the linker, meaning even if the C++ standard defined an initialization order, the order of initialization would be useless for practical purposes. Further, there are easy to use techniques using a function like
    Code:
    int getI() 
    {
           static int i = 4;
             
           return i;
    }
    When this function is called, even from a global context, the variable i is automatically initialized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  2. Assignment vs. Initialization
    By arrgh in forum C Programming
    Replies: 6
    Last Post: 05-06-2008, 04:08 AM
  3. initialize constant and initialization list
    By sawer in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2006, 07:30 AM
  4. class initialization and the = operator
    By krygen in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2005, 12:44 AM
  5. Why member initialization?
    By d00b in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 04:05 PM

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