Saturday 4 January 2014

Usage of new and delete keywords

//->new and delete keywords are used to assign and free memory dynamically

#include<iostream.h>
#include<conio.h>
void main()
{
int *arr,len;
clrscr();
cout<<"enter the size of array..";
cin>>len;
arr=new int[len];//allocates memory for array arr
for(int i=0;i<len;i++)
{
cout<<"enter array element.."<<i+1<<"\n";
cin>>arr[i];
}
cout<<"array elements are..";
for(i=0;i<len;i++)
{
cout<<"\t"<<arr[i];
}
delete arr;
getch();
}

No comments:

Post a Comment