Initialization - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

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

### [edit] Non-local variables

All 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:
b) 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 undeterminately sequenced with respect to all other dynamic initialization. Initialization of such thread-local variables is unsequenced with respect to all other dynamic initialization.
a) 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.

| This section is incomplete Reason: when ambiguous, examples

### [edit] Class members

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/language/initialization&oldid=67334"
Category:

- Todo with reason