answersLogoWhite

0


Best Answer

All objects must be initialised before we can use them. Initialisation can either occur statically (at compile-time) or dynamically (at runtime) however declaring an object static does not imply static initialisation.

Static initialisation relies on constant expressions. A constant expression is any expression that can be computed at compile-time. If a computation involves one or more function calls, those functions must be declared constant expression (constexpr) in order to take advantage of compile-time computation. However, whether or not the computation actually occurs at compile-time or not is implementation-defined. Consider the following code:

constexpr unsigned f (unsigned n) { return n<2?1:n*f(n-1); }

static unsigned num = f (4); // e.g., num = 4 * 3 * 2 * 1 = 24

int main () {

// ...

}

The above example is a potential candidate for compile-time computation. A good compiler will interpret this code as if it had been written:

static unsigned num = 24;

int main () {

// ...

}

However, if the function f () were not declared constexpr, the compiler would instead interpret the code as if it were actually written as follows:

unsigned f (unsigned n) { return n<2?1:n*f(n-1); }

static unsigned num; // uninitialised!

int main () {

num = f (4);

// ...

}

In other words, num is dynamically initialised -- at runtime.

Local objects are always initialised dynamically, as are objects allocated upon the heap (the free store). Static objects may be statically allocated, however this depends on the complexity of the object's constructor as well as the compiler's ability to perform compile-time computation.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do we need dynamic initialization of objects in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the order of initialization of data in C plus plus?

The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code


What do you mean by initialisation of objects in c plus plus?

Initialization of objects means to provide an initial value for the object. This is usually done by the constructor, or it can be done with an assignment statement.


Initialization of variables in namespace in C plus plus?

Is an important thing to do.


C plus plus uses dynamic memory management?

No, C++ does not use dynamic memory management. The programmer is entirely responsible for releasing dynamic memory when it is no longer required. When static objects fall from scope, their destructors are called automatically, but there is no automatic garbage collection for dynamic objects. Allocated memory remains allocated until the programmer manually releases it, or the thread that owns the memory is terminated.


What are the various concepts of c plus plus?

1.Classes and Objects 2.Constructors and Destructors 3.Inheritance 4.Polymorphism 5.Dynamic Binding


What is the difference between instantiation and initialization in C plus plus?

Instantiation is creating the instance of the variable/object . While Initialization is to provide the variable with some value. int i; // i is an instance of an integer i=10; //initialised with the value 10


What do you mean by initialization in c plus plus?

Not initialized variable: int myInt; Initialized variable: int myInt = 10;


How is dynamic initialisation achieved in c plus plus?

The C++ standard has this to say about dynamic initialisation:"Objects with static storage duration shall be zero-initialised before any other initialisation takes place. Zero-initialisation and initialisation with a constant expression are collectively called static initialisation; all other initialisation is dynamic initialisation."


How do you avoid garbage values in c plus plus programs?

Initialization. Hint: your compiler might warn you, neverignore warnings if you aren't absolutely sure what they mean.


Static vs dynamic binding in c plus plus?

Static binding occurs at compile time. Dynamic binding occurs at runtime.


How do you import a whole c plus plus class from a dynamic library?

You do not import classes. Classes are the definition of a type, and if you link to a library then all of its types are made available to your program. All you have to do is instantiate objects from those types: thus there is nothing to import.


What do you mean by read only objects in c plus plus?

Objects that are not supposed to be written. Surprised?