Default Initialization is when a variable is initialized without explicit value. They could be in an uninitialized state and accessing them could be an undefined behavior.
POD types like int, float are initialized to an intermediate value. Accessing them without initialization is an undefined behavior,
User defined types should either have no constructor provided, or have a default constructor present.
1
2
3
4
5
6
7
8
classA{A(){};// This is NOT default initializationA()=default;// This IS default initialization};classB{intvalue;// There's a synthesized default initializer as well.};
static storage duration always default initializes to 0, whereas automatic storage variables are default initialized to intermediate values
1
2
3
4
5
6
7
int x; // automatic storage, intermediate value
static int y; // default initialized to 0
int *j; // automatic storage, intermediate value
static int *p // static storage, default initialized to nullptr
int arr[5]; // Elements have indeterminate values
static int arr[5]; // All elements default-initialized to 0
Best Practices:
Use brace initialization{} (C++ 11) to explicitly initialize variables
1
inti{};// explicitly initialized to 0;
Initialization Order
We need to make sure the vairable order is consisntent in both the initializer list and the variable definition. One common warning we see is warning: <VAR> will be initialized after [-Wreorder]
1
2
3
4
5
6
7
8
9
10
11
12
classMyClass{/*
* Below public functions are defined in the order of operation
*/public:// Step 0: Create this object when first powered onMyClass():previous_time_(millis()),current_status_(Status::UNINITIALIZED){}Statuscurrent_status_;unsignedlongprevious_time_;};