C++ Program to show use of new and delete Operators.
#include <iostream>
using namespace std;
int main()
{
int i,*p; //pointer variable
p=&i; //pointer now points to i
p=new int [3]; //new operator
for(i=0; i<3; i++)
{
cout<<"Enter Value "<<(i+1)<<": ";
cin>>(*(p+i)); //incrementing pointer address
}
for(i=0; i<3; i++)
{
cout<<"\nValue: "<<(*(p+i));
cout<<"\nAddress: "<<(unsigned)p+i;//address of value
}
cout<<"\n\tdelete operator:";
delete []p; //delete operator
cout<<"\nMemory Released";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i,*p; //pointer variable
p=&i; //pointer now points to i
p=new int [3]; //new operator
for(i=0; i<3; i++)
{
cout<<"Enter Value "<<(i+1)<<": ";
cin>>(*(p+i)); //incrementing pointer address
}
for(i=0; i<3; i++)
{
cout<<"\nValue: "<<(*(p+i));
cout<<"\nAddress: "<<(unsigned)p+i;//address of value
}
cout<<"\n\tdelete operator:";
delete []p; //delete operator
cout<<"\nMemory Released";
return 0;
}