Initialization of a variable provides its initial value at the time of construction.
The initial value may be provided in the initializer section of a declarator or a new expression. It also takes place during function calls: function parameters and the function return values are also initialized.
For each declarator, the initializer may be one of the following:
( expression-list ) | (1) | |
= expression | (2) | |
{ initializer-list } | (3) |
Depending on context, the initializer may invoke one of the following:
- Value initialization, e.g.
std::string s{};
- Direct initialization, e.g.
std::string s("hello");
- Copy initialization, e.g.
std::string s = "hello";
- List initialization, e.g.
std::string s{'a', 'b', 'c'};
- Aggregate initialization, e.g.
char a[3] = {'a', 'b'};
- Reference initialization, e.g.
char& c = a[0];
If no initializer is provided, the rules of default initialization apply.
Non-local variables
All non-local variables with static storage duration are initialized as part of program startup, before the execution of the main function begins (unless deferred, see below). All variables with thread-local storage duration are initialized as part of thread launch, sequenced-before the execution of the thread function begins. For both of these classes of variables, initialization occurs in two distinct stages:
Static initialization
.bss
segment of the program image, which occupies no space on disk, and is zeroed out by the OS when loading the program.Dynamic initialization
After all static initialization is completed, dynamic initialization of non-local variables occurs in the following situations:
If the initialization of a non-local variable with static or thread storage duration exits via an exception, std::terminate
is called.
Early dynamic initialization
The compilers are allowed to initialize dynamically-initialized variables as part of static initialization (essentially, at compile time), if the following conditions are both true:
Because of the rule above, if initialization of some object o1
refers to an namespace-scope object o2
, which potentially requires dynamic initialization, but is defined later in the same translation unit, it is unspecified whether the value of o2
used will be the value of the fully initialized o2
(because the compiler promoted initialization of o2
to compile time) or will be the value of o2
merely zero-initialized.
inline double fd() { return 1.0; } extern double d1; double d2 = d1; // unspecified: // may be statically initialized to 0.0 or // dynamically initialized to 0.0 if d1 is dynamically initialized, or // dynamically initialized to 1.0 if d1 is dynamically initialized double d1 = fd(); // may be initialized statically or dynamically to 1.0
Deferred dynamic initialization
It is implementation-defined whether dynamic initialization happens-before the first statement of the main function (for statics) or the initial function of the thread (for thread-locals), or deferred to happen after.
If the initialization is deferred to happen after the first statement of main/thread function, it happens before the first odr-use of any variable with static/thread storage duration defined in the same translation unit as the variable to be initialized. If no variable or function is odr-used from a given translation unit, the non-local variables defined in that translation unit may never be initialized (this models the behavior of an on-demand dynamic library). However, as long as anything from a TU is odr-used, all non-local variables whose initialization or destruction has side effects will be initialized even if they are not used in the program.
// - File 1 - #include "a.h" #include "b.h" B b; A::A(){ b.Use(); } // - File 2 - #include "a.h" A a; // - File 3 - #include "a.h" #include "b.h" extern A a; extern B b; int main() { a.Use(); b.Use(); // if a is initialized before main is entered, b may still be uninitialized // at the point where A::A() uses it (because dynamic init is indeterminately sequenced // across translation units) // If a is initialized at some point after the first statement of main (which odr-uses // a function defined in File 1, forcing its dynamic initialization to run), // then b will be initialized prior to its use in A::A
Static local variables
For initialization of locals (that is, block scope) static and thread-local variables, see static local variables.
Class members
Non-static data members can be initialized with member initializer list or with a default member initializer.
Notes
The order of destruction of non-local variables is described in std::exit
.
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 2026 | C++14 | zero-init was specified to always occur first, even before constant-init | no zero-init if constant init applies |
See also
C documentation for initialization |
Please login to continue.