Dynamic Memory Allocation in C++

Dynamic Allocation is the means by which a program can get memory during runtime.The global and local variables are allotted memory during compile-time. Still, you cannot add any global or else local variables throughout runtime. if, your program wants to use a variable amount of memory you would want to assign memory during runtime, as and when required. Hence the dynamic allocation routines can work for the purpose.

C++ dynamic allocation routines get memory for allocation from the free store, the pool of unallocated heap memory only if to the program. C++ describes two unary operator new and delete.theyexecute the task of allocating and freeing memory during runtime. they are also named free store operator.

new operator

  • Placement new is a distinction newoperator in C++. Normal new operator organizes two things :

(1) Assigns memory

(2) Creates an object in allocated memory.

  • In placement new, we can pass a preallocated memory as well ascreate an object in the passed memory.


new (address) (type) initializer
As we can see, we can state an address
where we want a new object of given type 
to be constructed.

As it agrees to construct an object on memory that is previouslyallocated , it is mandatory for optimizations as it is sooner not to re-allocate all the time. There may be cases when it is compulsory to re-construct an object many times so, placement new operator might be more effectual in these cases.

Delete operator

this can only delete the storage created in heap, consequently when placement new is used delete operator cannot be used to delete the storage. In the case of memory allocation using placement new operator , for the time being it is formed in stack the compiler recognizes when to delete it and it will handle deallocation of the memory repeatedly





Constructor : (4.2, 5.3)
Constructor : (0, 0)
Constructor : (0, 0)
Constructor : (2.6, 3.9)
|4.2 +j5.3 | = 6.7624
|0 +j0 | = 0
|0 +j0 | = 0
|2.6 +j3.9 | = 4.68722
Destructor : (4.2, 5.3)
Destructor : (0, 0)
Destructor : (0, 0)
Destructor : (2.6, 3.9)

Here the destructor is clearly called since here it cannot be wrapped within the delete operator as delete will need to release the memory which you do not have here.it cannot be implicit as it is a dynamic process which we need to achieve yourself.

Leave A Comment

Your email address will not be published. Required fields are marked *