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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More