Dynamic Memory Allocation in C++

Normally, while using source codes in C++ programming language, a compiler allocates the memory manually to the variable for the storage of data. It is said to be an allocation of static memory. This is a fixed memory that cannot be changed once declared. For this type of memory allocation, the operating system uses the stack to store data. In static allocation, the memory is allocated before the source code starts to execute.

Whereas, in the dynamic memory allocation, the memory is allocated while the execution has started. This memory is allocated manually by the programmer at run-time, also known as a run-time memory allocation in C++. The size of dynamic memory can be changed at any position in the program because at the time of declaration, we do not mention a size that can be fixed. We only provide the value directly to the variable.

Difference of memory allocation to normal variables

In normal variables, the memory that is allocated by a compiler is allocated and deallocated automatically. When the memory is allocated dynamically by the programmer, he then has to remove or deallocate the memory when it is of no use in the further execution of the source code. This situation causes a ‘memory leak’ when the program is terminated while the memory is not deallocated.

Operators for dynamic allocation

In C++, two operators help in memory allocation and deallocation: ‘new’ and ‘delete’ that are used for the allocation and deallocation of the memory in a better way.

New operator

It signifies the demand for memory allocation. The new operator initializes the memory and returns the address of that allocated memory to the pointer variable if there is enough memory available.

Pointer object = new data-type;

Delete operator

Just like the new operator, a delete operator is used to remove the allocated memory. In C++, programmer can use this operator for deallocation.

# Delete pointer_variable;

Example 1

In this example, we will introduce two pointers: one is an integer type pointer and the other is a float pointer. Pointers are initialized by using an asterisk sign with them.

# Int * pointInt;
# Float *pointfloat;

By using these two printers, we will dynamically allocate the memory.

Role of pointers in dynamic allocation:
The memory of storage space is developed in the form of blocks. Whenever we execute a program or perform any operation, the memory is allocated for that specific purpose. That memory has a special address that is associated with the program that identifies which process or a program is allowed to that memory. Any memory slot is accessed through the address to which it belongs. So this address is stored through the pointers. In short, we need pointers to access memory and in the same way, to allocate a specific part of memory to any task. Pointers are needed to store addresses.

As the ‘new’ keyword is used for the dynamic allocation of memory in manual allocation, the memory is allocated by the compiler. We do not need to allocate memory at run time. But as dynamic allocation is random, we need to identify the pointers and for the binding process, this new operator is used.

# Pointint = new int;

Similarly, the floating pointer is bound likewise. After the binding process, we will assign any value to the memory that we want to book for any operation. By declaring the pointer, we assign a specific value to the memory.

# *pointInt = 50;

A float value for the point floats is also declared. Display the values after assigning.

As we have discussed, the ‘new’ operator is used to allocate while ‘delete’ is used to deallocate memory. So once you have completed the task or operation in the code, we will remove the memory we have allocated to the task.

It is better to deallocate that part of the memory so that any other process can avail this. We will apply this allocation to both of the pointers.

Delete point float;

Once you save the code on the text editor, the Ubuntu terminal allows you to execute the source code inside the file through a g++ compiler.

$ g++ -o mem mem.c
$ ./mem

Upon execution, you will see the values assigned to the memory.

Example 2

This example has the involvement of user interaction. We will take a number variable that will contain a value from the user. This program will store the result in the GPA of the students. All the outcomes will be saved at run time.

When the user enters the number of students, memory is allocated against each number. A float type pointer is initialized here that will be used in the memory allocation of the results.

We take the pointer in float since GPA is in decimal notation. We take a pointer type array for the GPA since it can result to a number of students.

Ptr= new float[num]

This pointer array with the ‘new’ keyword will bind the execution with the memory. The GPA will be entered for each student. As we are not familiar with the number of students the user wants to add, we used a for loop to enter the GPA up to the entered number. In each repetition of the loop, the user is inquired to enter the result identifying the student. Once the result is saved, we will again use a loop to display all the GPAs of the students. In the end, the pointer type array is deleted, as the purpose of dynamic storage was accomplished.

Delete [] ptr;

Now we will execute the above-mentioned code. The user will be first requested to enter the number of students. Then the GPA for each student will be entered.

Example 3

This example uses the new and delete operators for the object of the class. This class contains a private variable of integer type that stores the age. In the public part of a class, the constructor is created that will initialize the age to a number ’10’. Another function is used here that will display the age that is initialized in the constructor.

Now we will go towards the main program for the dynamic allocation. The object of the class is created dynamically.

Student * ptr = new student ();

When the object is formed, the constructor will be implemented automatically. A function call will be made to get the age. This will be done through the ptr.

Ptr -> getAge();

And at the end, the memory will be released.


Dynamic memory allocation is allocated at the run time execution by the programmer instead of fixed storage identified by the compiler. This allocation is on a random basis and can be eliminated after it is used. Whereas, in most cases, before the removal, the execution process stops and this dynamic allocation then causes the memory leaks. We have implemented this phenomenon in different approaches in the Ubuntu Linux system using C++ programming language.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.