What is dynamic object initialization in C ++?

What is dynamic object initialization in C ++?

Please explain with a simple example ...

+33
c ++ object initialization dynamic
May 10 '11 at 6:07 am
source share
4 answers

Dynamic initialization is that the initialization value is unknown at compile time. It was 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 computed at runtime. 

That is, static initialization usually includes a constant expression (which is known at compile time), while dynamic initialization includes a non-constant expression.

 static int c;//this is also static initialization (with zero)! 

Β§3.6.2 / 1 of the C ++ (2003) standard states:

Objects with a static storage duration (3.7.1) must be zero (8.5) before any other initialization occurs. Zero initialization and initialization with an expression constant are called collectively static initialization ; all other initialization dynamic initialization .

So there are two types of initializations:

  • Static initialization: either zero initialization or constant expression initialization
  • Any other initialization is dynamic initialization.

Also note that the same variable can be dynamically initialized after static initialization. For example, see this code:

 int d = factorial(8); int main() { } 

Since d is a global variable, it has static storage. This means that according to Β§3.6.2.1 it is initialized to 0 at the stage of static initialization, which occurs before any other initialization occurs. Then later at runtime, it is dynamically initialized with the value returned by factorial() .

This means that global objects can be initialized twice: once with static initialization (which is zero initialization), and later, at run time, they can be dynamically initialized.

+43
May 10 '11 at 6:11
source share

Dynamic initialization means the first value assigned to a variable after allocating memory at compile time, it is evaluated only at run time. eg

 #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 you know, a constant can get a value only once, that is, during initialization. this example shows that even a global variable, which is a static storage, if dynamically initialized by the return value of the function, the first value assigned to the variable is the value returned by the function, which replaces the initial default value 0 of the variable that is assigned during memory allocation.

+6
Sep 22
source share

Initializing a variable at runtime with the keyboard is called dynamic initialization.

Program Code: -

  int a=cube(n); 

In the above code, a is a global variable for which the number n dynamically assigned through the cube function, where cube() executes the cube of the number.

This is an example of dynamic initialization.

0
Sep 23
source share

Dynamic initialization means that initial values ​​can be provided at runtime. Even class objects can be dynamically initialized. That is, with the values ​​specified at runtime. :-))

0
Jul 04 '13 at 15:31
source share



All Articles