C++ Memory Management4 min read

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance.

Requirement of Memory Management.

We know that data in arrays are stored in a homogeneous way. And we allocate the memory during the array declaration. But in some situations, we may not know how much memory to allocate or how much space the data may require until runtime, so to overcome this situation we declare an array with a maximum size but here the memory is unused and wasted. So to increase the array size, we can manually allocate the memory during run-time and this allocating and freeing of memory refers to dynamic memory allocation in C++ programming.

Now, we know that in C programming, we use malloc() or calloc() functions to dynamically allocate the memory to a variable and free() function is used to free the memory. The same task is obtained in C++ with the use of new and delete Operators. Let us understand them individually.


C++ new Operator.

Just like malloc() or calloc(), the new operator is used to allocate the memory to a variable and arrays. The following is the way of using the new operator.

We use malloc() function in C, but it still exists in C++ but we avoid the use of malloc() in C++. The new operator has its own advantages, the main advantage is that it creates a new object in C++ which is the main concept of C++ as it involves OOP.

It is also a Good practice to check if the free store has been used or not. You can check in the following manner.


C++ delete Operator

We use delete to free up dynamically allocated space that is to delete the dynamic memory once we no longer required it. It means deallocating the memory. The syntax for the delete operator is:

Let us go through a simple C++ example to understand better.

Output:

You can also see we deallocated the memory at the end after its use.


new and delete Operators for Arrays

Now let us see the use of new and delete operators in arrays. Dynamic allocation increases the efficiency of memory management, especially in arrays.

The syntax or way to allocate memory and deallocate memory is done in the following way in c++.

The syntax is for one dimensional array. You can also allocate memory to multi dimensional array in the following way:

As you can see deallocation is same for both one and multi dimensional arrays.

Now let us see an C++ example for dynamic memory allocation in array.

Output:


new and delete Operators for Objects

As said earlier, a new operator is also used to create new objects in C++. Let us understand through an example.

Output:

In the above program, we created a Employee class where salary variable is initialized using the constructor. An in the main function, we created an object of the Employee class using a new operator and used a pointer to point to the address.

As soon as we create the object the salary is initialized by the constructor and we use the following code to call the function empSalary() from the Employee class


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …