Initialization is a processing assigning an initial value to a variable while declaring it. For example,

int age = 12;

When the compiler encounters this, it allocates the memory to the variable and assigns the value to it.

In c++ there are various ways to initialize variables, each of having their own quirks and benefits.

Forms of initialization in C++

There are 5 common forms.

int a; // default-initialization
 
// traditional initialization forms
int a = 1; // copy-initialization
int a (1); // direct-initialization
 
 
// modern initialization forms
int a {}; // value-initialization (or zero-initilization if garbage value is zero)
 
int a {1}; // direct-list-initialization

copy-initialization

Value on the right hand size is copied to the variable. This form is usually less efficient. However, in old code this form usually used as it easy to read.

direct-initialization

Initialization value is provided in paranthesis.

list-initialization

Curly braces are used for initialization. These forms are commonly used now a days. It also has two forms.

int a {3};
 
// and
 
int a = {3}; // copy-list-initialization

copy-list-initialization is rarely used.

Advantage of list-initialization is that it disallows narrowing conversions and violations of that leads to compile time errors or warning. For example,

int a {4.5}; // gives compile time error/warning

However, reassigning value using copy-assignment later does narrowing conversions.

int a {4.5};
a = 5.5; // no issues

value-initialization

This form is used when no initial value is specified. In this case, variable will most likely to initialized to zero (or closet to zero). If zero is initialized, this is called zero-value-initialization.

References

  1. https://www.learncpp.com/cpp-tutorial/variable-assignment-and-initialization/