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

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *