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++ Structures

In the C/C++ programming language, the structure is user-defined data types that allow us to store the combination of different data types together. All the …
Read More

C++ Static Keyword

Static s a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a …
Read More

C++ Templates

Templates in C++ are the powerful feature that allows us to write generic programs (generic classes and generic functions). A single class or function is …
Read More

C++ Friend Function

There are private and protected members in a class that is inaccessible from outside of the class, constituting one of the concepts of object-oriented programming …
Read More

this Pointer in C++

In C++, this is a keyword that refers to the current instance of a class and this pointer holds the address or points to the …
Read More

Inheritance in C++

Inheritance is one of the most important concepts of object-oriented programming. In C++, inheritance is the process of deriving the properties and behaviors of one …
Read More