Introduction
Any computer program written in any language needs some memory in order to be executed. In C++, dynamic memory allocation refers to manually allocating memory by the programmer. Many times, the programmer is not aware of the amount of memory required to store information, or the number of data items keeps changing. In such a scenario dynamic memory comes in handy.
Dynamically we can allocate memory while the program is still running, but the variables cannot be created during the execution of the program, because of which there are two criteria for dynamic memory allocation:
- A section in memory is needed which can be used as a dynamic space
- We need to store the address so that we can access the variable from memory when required.
Apart from allocating memory, we also need to de-allocate the memory which was earlier allocated. It is the responsibility of the programmer to de-allocate dynamically created space, it is not done by the compiler.
For e.g., when we create a normal variable like ‘int x’ memory is automatically allocated and de-allocated without any interference of the programmer, but when we allocate memory dynamically like ‘int *x=new int[5]’, now it is the programmer's responsibility to de-allocated the memory allocated for it when it is no longer needed.
Also see, Literals in C.Fibonacci Series in C++
New & Delete Operators
The new operator is a special operator used in C++ to allocate memory at run time within the heap for the variable of a given type, and it returns the address of the space allocated. The new operator basically denotes a request for memory allocation, if sufficient memory is available, it initializes the memory and returns the address of the allocated memory.
Since it is the responsibility of the programmer to de-allocate the memory, the delete operator is used. Whenever a programmer feels that the variable that has been dynamically allocated is not required anymore, the programmer can free the memory occupied by the variable using the ‘delete’ operator.
Syntax
pointerVariable = new dataType;
New operator can be used with any built-in data type as well as custom data types also, which are defined using class or structure to allocate dynamic memory.
E.g., int *x=new int
Because C++ is the superset of the C programming language, it also has malloc() function, but it is preferred to use new over malloc(), as new allocates memory as well as constructs objects which is the prime purpose of C++ being an object-oriented programming language.
Syntax
delete pointerVariable;
Example
#include <bits/stdc++.h>
using namespace std;
int main () {
int* x = NULL; // initialised with null
x = new int; // Request memory for the variable
*x = 12; // Store value at allocated address
cout << "Value of X : " << *x << endl;
delete x; // free up the memory.
return 0;
}
Output
Value of X : 12
Explanation
In the above example, we have first created a pointer named ‘x’ and initialized it with null. Then using the ‘new’ operator, we have requested memory for the variable, and after that, we have assigned it the value ‘12’. Once the variable is used and is no longer required, we de-allocate the memory, which was allocated by ‘x’.
Try and compile with online c++ compiler.