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

Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More