Initialization

From cppreference.com
< cpp‎ | language

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)
1) comma-separated list of arbitrary expressions and braced-init-lists in parentheses
2) the equals sign followed by an expression
3) braced-init-list: possibly empty, comma-separated list of expressions and other braced-init-lists

Depending on context, the initializer may invoke one of the following:

If no initializer is provided, the rules of default initialization apply.

Contents

[edit] 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. All variables with thread-local storage duration are initialized as part of thread launch, before the execution of the thread function begins. For both of these classes of variables, initialization occurs in stages:

1) Static initialization of non-local variables happens first, and consists of two steps
a) Zero initialization takes place before anything else (in practice, variables that are going to be zero-initialized, are typically placed in the .bss segment, which is zeroed out by the OS when loading the program)
b) Constant initialization takes place where applicable. (in practice, this stage is usually performed at compile time, and pre-calculated values are stored as part of the program image. If the compiler doesn't do that, it still has to guarantee that this takes place before any dynamic initialization)
2) Dynamic initialization of non-local variables, which takes place after all static initialization is completed, can be one of:
a) Unordered dynamic initialization, which applies only to (static/thread-local) class template data members that aren't explicitly specialized. Initialization of such static variables is indeterminately sequenced with respect to all other dynamic initialization. Initialization of such thread-local variables is unsequenced with respect to all other dynamic initialization.
b) Ordered dynamic initialization, which applies to all other non-local variables: within a single translation unit, these variables are initialized in exact order their definitions appear in the source code. Initialization of static variables in different translation units is indeterminately sequenced. Initialization of thread-local variables in different translation units is unsequenced.

The compilers are allowed to initialize dynamically-initialized variables as part of static initialization (essentially, at compile time), if both of these are true:

1) the dynamic version of the initialization does not change the value of any other object of namespace scope prior to its initialization
2) the static version of the initialization produces the same value in the initialized variable as would be produced by the dynamic initialization if all variables not required to be initialized statically were initialized dynamically.

[edit] Static local variables

For initialization of locals (that is, block scope) static variables, see static local variables

[edit] Class members

Non-static data members can be initialized with member initializer list or with a in-class brace-or-equal initializer.

[edit] See also

C documentation for initialization