TITLE: Initialization of global objects PROBLEM: mbersohn@alchemy.chem.utoronto.ca (M. Bersohn) Does anyone know when global objects of a class with a constructor that does initializations are initialized, at compile time or at the beginning of execution? P.S. I'm aware that aggregates with an initialization list are initialized at compile time, but a constructor looks like code that is executed. RESPONSE: steve@taumet.com (Steve Clamage), 25 Jan 93 Global objects with constructors are initialized before the first statement of main() is executed. Within one compilation unit, they are initialized in definition order, except that objects with a default initialization to zero are initialized first. The C++ Committee is working on a way to specify that objects initialized with constant expressions get initialized before other objects. (Current implementations usually work this way anyhow.) "Compile-time initialization" is not a C++ concept (nor a C concept). For example, consider this entire translation unit: extern int i; int *ip = &i; This is legal code in C and C++, yet the compiler (assuming a separate linker) cannot provide the value for the initialization of "ip". Suppose further that "i" is in a shared, dynamically-linked library. We would not even have link-time initialization in that case. The address is not known until the program is loaded into memory and the program loader (not the object-code linker) finds the library. C requires that objects with static duration be initialized only with constant expressions (without going into all the details). C++ allows such objects to be initialized with anything which may be used to initialize a dynamic object of the same type. As noted above, current implementations usually arrange things so that initialization with constant expressions occurs prior to the program beginning execution, thus requiring no run-time code. Objects with non-constant initializers generally require run-time code which is part of the program.