Static Initialization and Dynamic Initialization

Static Initialization and Dynamic Initialization

C++ distinguishes between two initialization types for objects with static storage duration (global objects are an example of objects having static storage). Static initialization consists of either zero-initialization or initialization with a constant expression; any other initialization is dynamic initialization. These two types roughly correspond to compile-time initialization and runtime initialization. For example:

 int x = func();int main(){}

The global variable x has static storage. Therefore, it’s initialized to 0 at the static initialization phase (this is the default value of objects with static duration). The subsequent dynamic initialization phase initializes x with the value returned from the function func(). Note that func() must be invoked for that purpose


Share the Post: