What is dynamic initialization of objects in c++?
Please explain with an simple example...
Dynamic initialization of object refers to initializing the objects at a run time i.e., the initial value of an object is provided during run time. It can be achieved by using constructors and by passing parameters to the constructors.
The process of initializing a variable at the moment it is declared at runtime is called dynamic initialization of the variable. Thus, during the dynamic initialization of a variable, a value is assigned to execution when it is declared. Example: main() { Int a; cout<<“Enter Value of a”; cin>>a; int cube = a * a * a; }
Static Initialization: Here, the variable is assigned a value in advance. This variable then acts as a constant. Dynamic Initialization: Here, the variable is assigned a value at the run time. The value of this variable can be altered every time the program is being run.
int a=cube(n); In the above program code , a is a global variable to which a number n is dynamically assigned through a function cube , where cube() performs the cube of a number. This is an example of Dynamic Initialization.
Dynamic initialization is that in which initialization value isn't known at compile-time. It's computed at runtime to initialize the variable.
Example,
int factorial(int n) { if ( n < 0 ) return -1; //indicates input error else if ( n == 0 ) return 1; else return n * factorial(n-1); } int const a = 10 ; //static initialization //10 is known at compile time. Its 10! int const b = factorial(8); //dynamic initialization //factorial(8) isn't known at compile time, //rather it's computed at runtime.
That is, static-initialization usually involves constant-expression (which is known at compile-time), while dynamic-initialization involves non-constant expression.
static int c;//this is also static initialization (with zero)!
§3.6.2/1 from the C++ Standard (2003) says,
Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization.
So there are two kind of initializations:
Also note that the same variable can be dynamically-initialized after it has been statically-initialized. For example, see this code:
int d = factorial(8); int main() { }
Since d
is a global variable, it has static storage. That means, according to §3.6.2.1
it's initialized to 0 at the static-initialization phase which occurs before any other initialization takes place. Then later, at runtime, it's dynamically-initialized with the value returned from the function factorial()
.
That means, global objects can be initialized twice: once by static initialization (which is zero-initialization) and later, at runtime, they can be dynamically-initialized.
Dynamic initialization means the first value assigned to the variable after memory allocation is not known at compile time, it is evaluated only at run time. for example
#include <iostream.h> using namespace std; int sample() { int x; cin >> x; return x; } const int t = sample(); //dynamic initialization int p = sample(); //dynamic initialization void main() { cout << t; cout << p; }
As we know that a constant can get value only once i.e. at the time of initialization. this example shows that even a global variable which is static storage if dynamically initialize by return value of a function, the first value assigned to the variable is the value returned by function, which replaces the initial default value 0 of the variable which is assigned at the time of memory allocation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With