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.
1 2 3 4 5 6 7 8 | // declare an int pointer int* pointerVar; //using the new keyword for dynamic allocation pointerVar = new int; //value assigned to new allocate memory *pointerVar = 45; |
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.
1 2 3 4 5 6 7 | int* pointerVar = NULL; if(!(pointerVar = new double )) { cout << "Error Message" <<endl; exit(1); } |
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:
1 2 3 4 | delete pointerVar; //For arrays delete [] pointerVar; |
Let us go through a simple C++ example to understand better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; int main() { // Initailizing pointer with Null int* pointerVar = NULL; // allocating memory dnamically pointerVar = new int; // assigning value to the memory *pointerVar = 458; cout << "Its Value: " << *pointerVar << endl; // deallocating the memory delete pointerVar; return 0; } |
Output:
1 | Its Value: 458 |
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++.
1 2 3 4 5 | //Allocating memory pointerVar = new data-type[size]; //Deallocating memory delete [] pointerVar; |
The syntax is for one dimensional array. You can also allocate memory to multi dimensional array in the following way:
1 2 3 4 5 | //Allocating memory to multi dimensional array pointerVar= new double [row][col]; //Deallocating multi dimensional array delete [] pionterVar; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> using namespace std; int main() { int number; float* ptr; cout << "How many students are present in your class: "; cin >> number; //allocating memory ptr = new float[number]; cout << "Enter their Maths marks: " << endl; for (int i = 0; i < number; ++i) { cout << "Roll " << i + 1 << ": "; cin >> *(ptr + i); } cout << "\nEntered Marks for each student." << endl; for (int i = 0; i < number; ++i) cout << "Roll " << i + 1 << ": " << *(ptr + i) << endl; // releasing the ptr memory delete[] ptr; return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | How many students are present in your class: 5 Enter their Maths marks: Roll 1: 68 Roll 2: 90 Roll 3: 84 Roll 4: 77 Roll 5: 96 Entered Marks for each student. Roll 1: 68 Roll 2: 90 Roll 3: 84 Roll 4: 77 Roll 5: 96 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> using namespace std; class Employee { int salary; public: // constructor Employee() : salary(25000) {} void empSalary() { cout << "Salary: " << salary << endl; } }; int main() { // Employee Objects Dynamically Employee* ptr = new Employee(); //calling empSalary funciton from Employee class ptr->empSalary(); // releasing pointer delete ptr; return 0; } |
Output:
1 | Salary: 25000 |
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