A Program for 'Dynamic Memory Allocation' :-


#include<iostream.h>
#include<conio.h>

class cube
{
public:
cube()
{
cout<<endl<<"Constructor is envoked";
}
~cube()
{

cout<<endl<<"Destructor is envoked";
}
};
void main()
{
cube *p=new cube[3];
delete[] p;
getch();
}

Output:


Constructor is envoked
Constructor is envoked
Constructor is envoked
Destructor is envoked
Destructor is envoked
Destructor is envoked

			

What is Dynamic Memory Allocation ?

Programmers can dynamically allocate storage space while the program is running, but programmers cannot create new variable names "on the fly", and for this reason, dynamic allocation requires two criteria:


1. Creating the dynamic space in memory
2. Storing its address in a pointer (so that space can be accessed)

Memory de-allocation is also a part of this concept where the "clean-up" of space is done for variables or other data storage. It is the job of the programmer to de-allocate dynamically created space. For de-allocating dynamic memory, we use the delete operator. In other words, dynamic memory Allocation refers to performing memory management for dynamic memory allocation manually.
Previous Next