An example to illustrate Dynamic memory allocation and deallocation

#include <iostream.h>
#include <conio.h>
void main( )
{
int *ptr;
int i,n;
clrscr();
cout<<”Enter how many elements you want to enter\n”;
cin>>n;
ptr = new int[n];
cout<<”Enter”<<n<<” elements\n”;
for(i=0; i<n;i++)
cin>> ptr[i];
cout<<”You have entered \n”;
for(i=0; i<n;i++)
cout<< ptr[i]<<”\n”;
delete ptr;
getch();
}
Output :
Enter how many elements you want to enter
5
Enter 5 elements
1
2
3
4
5
You have entered :
1
2
3
4
5

0 comments: