Dynamic memory allocation in C++

Hello Learners, today we are going to learn a very important topic in C++ that is “Dynamic memory allocation. To use this tactic in C language we have different functions like calloc, malloc and free function. But in C++ we have new and delete operators for doing this task.

Before we continue to the topic, we will see how memory is classified in C++. C++ memory is classified into types that are stack and heap.

Dynamic memory allocation is nothing but allocating the memory in the run time, this done with the help of heap. The daily life application of dynamic memory allocation is that: Suppose in a company number of employees strength changes every day, so to store that data at run time we can use dynamic memory allocation concept to store the employee’s data.

Now let’s see how this dynamic memory allocation actually works with the help of a code.

C++ program for dynamic memory allocation

As we know that in C++ programming language, we have two operators for dynamic memory allocation that is new and delete operator. New operator is used to allocating the memory and delete operator is used to deallocate the memory at the run time.

Now let us see, the syntax for new and delete operators.

Syntax:

new data_type;delete data_type;

Code: 1) simple code addition of two numbers with the help of dynamic memory allocation.

#include<iostream>
using namespace std;

class dynamic_memeory_allocation
{
    private:
    int *a = new int;
    int *b = new int;
    public:
    void getdata()
    {
        cout<<"enter the value of a and b:";
        cin>>*a>>*b;
    }
    void displaydata()
    {
        cout<<"the numbers are:"<<*a<<endl<<*b<<endl;
    }
    void addition()
    {
        cout<<"addition of two numbers is:"<<*a+*b;
        delete a;
        delete b;
    }
};

int main()
{
    dynamic_memeory_allocation obj;
    obj.getdata();
    obj.displaydata();
    obj.addition();
    return 0;
}

Output:

enter the value of a and b:2
3
the numbers are:2
3
addition of two numbers is:5

code: 2) To check how the dynamic memory allocation works for arrays.

#include<iostream>
using namespace std;
class dynamic_memory_allocation
{ 
    private:
    int *ptr,n;
    public:
    void getdata()
    {
    cout<<"enter the size of the memory:";
    cin>>n;
    ptr = new int[n];
    cout<<"enter the elments to store in the memory:";
    for(int i=0;i<n;i++)
       {
        cin>>ptr[i];
       }
    }
    void displaydata()
    {
    cout<<"the elemnts in the memory are:";
    for(int i=0;i<n;i++)
        {
        cout<<ptr[i]<<"\t";
        }
     delete [] ptr;
    }
};

Output:

enter the size of the memory:4
enter the elments to store in the memory:23 
3 
12 
4
the elemnts in the memory are:23 3 12 4

code: 3) To check, the working of dynamic memory allocation for constructors and destructors.

#include<iostream>
using namespace std;
class dynamic
{
    public:
     dynamic()
    {
        cout<<"constructor called!"<<endl;
    }
     ~dynamic()
    {
        cout<<"destructor called!"<<endl;
    }
};
int main()
{
    int size;
    cout<<"enter the size to call constructor and destructor:";
    cin>>size;
    dynamic *obj=new dynamic[size];
    delete [] obj;
    return 0;
}

Output:

enter the size to call constructor and destructor:3
constructor called!
constructor called!
constructor called!
destructor called!
destructor called!
destructor called!

This is all about how dynamic memory allocation works in C++. If you have any doubts pls mention in the comment section below.

Also, read: Listing all files and sub-directories within a directory in C++

3 responses to “Dynamic memory allocation in C++”

  1. MAHINDRA REDDY SURAM - says:

    Explained in easy way and everyone can understand …. nice explaination

  2. Uppaluri Anirudh says:

    Thank you sir✨. It was really very helpful.

  3. U Anirudh says:

    Really very useful sir. Thank you so much

Leave a Reply