New and Delete operator in C++

In C++, the new operator is used to allocate the memory from free store and delete operator is used to return the previously allocated memory back to the free store.

 

We can say new and delete perform the job of  malloc()(dynamic memory allocation) and free()(perform deallocation) functions(of C)

 

 

 

#include<iostream.h>

#include<conio.h>

int main()

{

     clrscr();

     int *pa=new int;

     int *pb=new int;

     int *psum=new int;

     cout<<"Enter the integer values"<<endl;

     cin>>*pa>>*pb;

     cout<<"value of *pa="<<*pa<<endl;

     cout<<"value of *pb="<<*pb<<endl;

     *psum=*pa+*pb;

     cout<<"sum of integer values are="<<*psum<<endl;

     delete pa;

     cout<<"after deletion *pa="<<pa<<endl;

     delete pb;

     cout<<"after deletion *pb="<<pb;

     getch();

     return 0;

}

Output...

New and Delete operator in c++