Dynamic Memory Allocation in C++ Part – 1
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Dynamic memory allocation in C++
#include<iostream>
using namespace std;
int main()
{
system("cls");
int *ar,n,sum=0;
cout<<"Enter the limit: "<<endl;
cin>>n;
ar=new int[n]; /// allocation of dynamic memory
cout<<"Enter elements in array: "<<endl;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
for(int i=0;i<n;i++)
{
cout<<ar[i]<<endl;
sum=sum+ar[i];
}
cout<<"sum of elements : "<<sum;
delete ar; /// deallocation of dynamic memory
cout<<"------------------------------------";
return 0;
}If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

