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++:
These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways.
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.
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;
int *a = new int(20);
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.
To understand the concept of new and delete memory management operator in C++:
using namespace std;
//Allocates using new operator memory space in memory for storing a integer datatype
int *a= new int;
cout << " The Output is:a= " << *a;
//Memory Released using delete operator
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:
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: