Tuesday, October 21, 2014

allocate memory using new operator & deallocate using delete operator

#include<iostream.h>
void main()
{
    int i,n;
    int * p1;
    p1=new int[30]; //allocate memory using new operator
    cout<<"enter how many no.(But limit within 30 elements)?";
    cin>>n;
    cout<<"Enter elements=\n";
    for(i=0;i<n;i++)
    {
        cout<<"i=";
        cin>>p1[i];
    }
    cout<<"\n Output=\n";
    for(i=0;i<n;i++)
    {
       cout<<"\t->"<<p1[i];
    }
    delete []p1;  // deallocate memory using delete operator
}

No comments:

Post a Comment