Friday, 20 March 2015

Dynamic Object Creation in C++

Dynamic Object Creation in Cpp with examples:

Sometimes it is required that we need to create the objects dynamically and also it is required that we need to destroy them at the runtime.

Using C there are two dynamic memory allocation functions, 1. malloc () and 2. free () , that allocate storage from the heap which is also called the free store at runtime.

Coming to the C++ the constructor does not allow us to hand it the address of the memory to initialize, and for good reason.

 If you could do that, you might:

1. Then guaranteed initialization of objects in C++ wouldn’t be guaranteed.
2. Accidentally do something to the object before you initialize it, expecting the right thing to happen.
3. Hand it the wrong-sized object. And of course, even if you did everything correctly, anyone who modifies your program is prone to the same errors.

Improper initialization is responsible for a large portion of programming problems, so it’s especially important to guarantee constructor calls for objects created on the heap.

The answer is by bringing dynamic object creation into the core of the language. malloc () and  free ()  are library functions, and thus outside the control of the compiler. However, if you have an operator to perform the combined act of dynamic storage allocation and initialization and another operator to perform the combined act of cleanup and releasing storage, the compiler can still guarantee that constructors and destructors will be called for all objects.

Object creation in C++:

Two events occure when the object is created:

1. Storage is allocated for the class object.
2. The constructor is called to initialize that storage.

By now you should believe that step two always happens. Otherwise, the uninitialized objects will give us unpredictable bugs of the program. So to resolve the problem when the constructor is called when the object is created.
Coming to the allocation of the storage area, if the storage is in the stack area or if the storage is heap area is decided when the program begins,

Stack Storage:

The storage can be created on the stack whenever a particular execution point is reached. That storage is released automatically at the complementary execution point.

These stack-allocation operations are built into the instruction set of the processor and are very efficient.

However, you have to know exactly how many variables you need when you’re writing the program so the compiler can generate the right code.

Heap Storage:

The storage can be allocated from a pool of memory called the heap which is allocated dynamically, This is called dynamic memory allocation. The heap may be implemented by making calls for chunks of memory from the operating system.

If we want to allocate the memory dynamically, a function is called at the runtime.

To allocate this memory, a function is called at runtime; this means you can decide at any time that you want some memory and how much you need. And also we need to release the dynamically allocated memory when the scope of the memory is completed. Otherwise we may get memory leaks.

The static, the stack, and the heap areas are placed in a single contiguous piece of physical memory.

No comments: