Need for Memory Management operators
The concept of arrays has a block of memory reserved. The disadvantage with the concept of arrays is that the programmer must know, while programming, the size of memory to be allocated in addition to the array size remaining constant.
In programming there may be scenarios where programmers may not know the memory needed until run time. In this case, the programmer can opt to reserve as much memory as possible, assigning the maximum memory space needed to tackle this situation. This would result in wastage of unused memory spaces. Memory management operators are used to handle this situation in C++ programming language.
What are memory management operators?
There are two types of memory management operators in C++:
- new
- delete
These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways.
New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type.
General syntax of new operator in C++:
The general syntax of new operator in C++ is as follows:
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
For example:
int *a=new int;
In the above example, the new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable a holds the address of memory space allocated.
Dynamic variables are never initialized by the compiler. Therefore, the programmer should make it a practice to first assign them a value.
The assignment can be made in either of the two ways:
int *a = new int;
*a = 20;
or
int *a = new int(20);
delete operator:
The delete operator in C++ is used for releasing memory space when the object is no longer needed. Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory.
General syntax of delete operator in C++:
The general syntax of delete operator in C++ is as follows:
delete pointer variable;
In the above example, delete is a keyword and the pointer variable is the pointer that points to the objects already created in the new operator. Some of the important points the programmer must note while using memory management operators are described below:
- The programmer must take care not to free or delete a pointer variable that has already been deleted.
- Overloading of new and delete operator is possible (to be discussed in detail in later section on overloading).
- We know that sizeof operator is used for computing the size of the object. Using memory management operator, the size of the object is automatically computed.
- The programmer must take care not to free or delete pointer variables that have not been allocated using a new operator.
- Null pointer is returned by the new operator when there is insufficient memory available for allocation.
Example:
To understand the concept of new and delete memory management operator in C++:
#include <iostream>
using namespace std;
void main()
{
//Allocates using new operator memory space in memory for storing a integer datatype
int *a= new int;
*a=100;
cout << " The Output is:a= " << *a;
//Memory Released using delete operator
delete a;
}
The output of the above program is
In the above program, the statement:
int *a= new a;
Holds memory space in memory for storing a integer datatype. The statement:
*a=100
This denotes that the value present in address location pointed by the pointer variable a is 100 and this value of a is printed in the output statement giving the output shown in the example above. The memory allocated by the new operator for storing the integer variable pointed by a is released using the delete operator as:
delete a;