42

What is dynamic initialization of objects in c++?

Please explain with an simple example...

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
Mahi
  • 463
  • 1
  • 6
  • 7

4 Answers4

57

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:

  • Static initialization : Its either zero-initialization or initialization with a constant expression
  • Any other initialization is dynamic initialization.

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • thanks nawaz.but i am a bit confused,can u do it with a small program. – Mahi May 10 '11 at 06:15
  • @Mahi: What do you find confusing seeing the example I've given? – Nawaz May 10 '11 at 06:18
  • 1
    @Mahi In dynamic initialization you calculate the value of the variable at runtime. In static initialization you set it at compile-time. In the case above assume that int f() returns a random "int" then the value of b is only know at runtime and is thus dynamically initialized. – Pepe May 10 '11 at 06:21
  • 2
    @Nawaz: note that there is an idea about super-compilation whose goal would be to run pure functions in a "late" compilation phase (actually, probably at link stage), to get even faster binaries. I don't know of any in C++ (purity isn't annotated...) but there have been various forays with Haskell. – Matthieu M. May 10 '11 at 06:41
  • @Matthieu: That sounds interesting. :-) C# does lots of such initialization at compile-time. – Nawaz May 10 '11 at 06:43
  • 2
    @Mahi: "Thanks Nawaz.i am clear now." is best expressed as a green tick next to the answer that helped you the most. See the [FAQ](http://stackoverflow.com/faq#howtoask) – johnsyweb May 16 '11 at 11:03
  • @Nawaz, +1. Don't suppose you happen to know where I can find out if GNU and Forte compilers perform dynamic initialization prior to main()? The Microsoft compilers do (based on this http://msdn.microsoft.com/en-US/library/0e519f64%28v=vs.80%29). I have searched and am searching but not found any official statement. Tests with GNU on sparc/x86 and forte on space/x86 both suggest they do. However, official documentation is required. – hmjd Sep 07 '12 at 13:43
  • @hmjd: I think this topic will interest you : [Is main() really start of a C++ program?](http://stackoverflow.com/questions/4783404/is-main-really-start-of-a-c-program). It discusses dynamic initialization *before* entering into `main()` function. :-) – Nawaz Sep 07 '12 at 14:12
  • I gotta -1 for `MAGIC_FAILURE_VALUE`. – Puppy Sep 23 '12 at 16:04
  • @DeadMG: What is that? Elaborate. – Nawaz Sep 23 '12 at 16:29
  • Your factorial value returns -1 on failure. This is an extremely bad error code- not only is it an error code, which is very bad, but it's the same place as the regular return value, which is even worse. You need to throw an exception if you don't explicitly know how to handle the error. – Puppy Sep 23 '12 at 18:09
  • 1
    @DeadMG: How good the code is, is not the point here. It is not a code to be used in production. But then some people here *are* crazy. They want a production code as "sample" code, without even realizing that *sample* code is used to demonstrate *other* concepts. – Nawaz Sep 23 '12 at 18:27
  • Hmm... considering C++11 & C++14 have been out for a while now, do you think it would be worth pointing out that theoretically speaking, a `constexpr` function could be used for both static and dynamic initialisation, depending on whether its parameters are compile-time constants? – Justin Time - Reinstate Monica Jun 15 '16 at 21:59
  • @JustinTime: If I understand your question as you meant it, then yes. – Nawaz Jun 16 '16 at 04:48
  • To be clear, I meant that, to my understanding: If a `constexpr` function is supplied with parameters that allow it to be evaluated at compile time, it can be used for static initialisation. If a `constexpr` function is supplied with parameters that require it to be evaluated at run time, it can only be used for dynamic initialisation. – Justin Time - Reinstate Monica Jun 16 '16 at 21:00
6

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.

Arun Kumar
  • 61
  • 1
  • 2
0

Initialization of a variable at the run time from the keyboard is known as Dynamic Initialization.

Program code:-

 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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

The dynamic initialization means that the initial values may be provided during run time. Even class objects can be initialized dynamically. I.e. with the values provided at run time. :-))