C++ allows us to allocate the memory during runtime for arrays or variables. This is known as dynamic memory allocation.
In languages like Python and Java, the memory allocated during runtime is managed automatically but in C++, we need to deallocate the memory which is allocated during runtime if we have no use of that array or variable.
C++ provides us with two operators
- New operator – It allows dynamic memory allocation.
- Delete operator – It allows dynamic memory deallocation.
C++ New operator
The new operator allows us to allocate memory to a variable during runtime.
Here is an example code
int *var; // Integer pointer is created var = new int; // memory allocate dynamically using new keyword *var = 9; // assign 9 to variable
Here we can see that an int pointer is created to allocate the memory dynamically for an int variable.
The new operator returns the address of the memory location.
For array, the new operator returns the address of the first cell of the array.
The syntax for the new operator
Variablepointer = new datatype;
C++ Delete operator
If there is no need for a dynamically allocated variable, so we need to deallocate its memory also. So, for this purpose, the delete operator is used. This is known as dynamic memory allocation.
The syntax for delete operator
delete Variablepointer;
Let us see the proper code and how this dynamic memory allocation works
Code:
C++ Code
int *var; // Integer pointer is created
var = new int; // memory allocate dynamically using new keyword
*var = 9; // assign 9 to variable
cout<<var<<endl; // print the variable memory location
cout<<*var<<endl; // print the value assigned to the variable
delete var; // deallocate the memory
One question that comes to our mind that why deallocation of memory allocated is required?
Since if we allocate the memory using the new operator, so after some time, there will be no memory available for the operating system. So, it will lead to System Crash. So always deallocate memory if it is of no use.
Not for Only Int, we can allocate memory for float variable also
Code:
C++ Code
#include<iostream>
using namespace std;
int main()
{
float *var; // float pointer is created
var = new float; // memory allocate dynamically using new keyword
*var = 9.3f; // assign 9 to variable
cout<<var<<endl; // print the variable memory location
cout<<*var<<endl; // print the value assigned to the variable
delete var; // deallocate the memory
return 0;
}
C++ new and Delete operator for Arrays
Here is the code to implement marks of all the students in the Mathematics subject.
Code:
C++ Code
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int* ptr;
// memory allocation of array
ptr = new int[n];
for (int i = 0; i < n; i++) {
cin >> *(ptr + i);
}
cout << "Marks of all students in Mathematics" << endl;
for (int i = 0; i < n; i++) {
cout << *(ptr + i) << endl;
}
// ptr memory is deallocated
delete[] ptr;
return 0;
}
Here pointer ptr is pointing to the first memory location of the array. Delete [] is used to deallocate the memory allocated to the array. Here [] (square brackets) are used to show that memory deallocated is of the array.
C++ new and Delete operator for Objects
Code:
C++ Code
#include <iostream>
using namespace std;
class Employee {
private:
int age;
public:
// constructor
Employee(int a)
{
age=a;
}
// Function to return age of the Employee
int getAge() {
return age;
}
};
int main() {
// dynamically allocated Employee object
Employee* ptr = new Employee(40);
cout<<"Age of given Employee is :"<<ptr->getAge()<<endl;
// deallocating Employee object
delete ptr;
return 0;
}
Employee class is created having the private member as age and a public function getAge() which returns the age of the Employee. Constructor is used to allocating the age of the Employee here. Here Employee object is created dynamically and when the age of the employee is printed then it is of no use. So its memory is deallocated using the delete operator just like variables.
Special thanks to Gurmeet Singh for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article