There are two memory management operators in C++:
1. new
2. delete
These two memory management operators are used for allocating and de-allocating memory blocks. C++ allow dynamic allocation techniques when it is not known in advance how much of memory space is needed.
New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type.
Syntax:
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
Example:
1. int *a = new int;
2. *a = 10;
or
3. int *a = new int(10);
In the above example, the new operator allocates sufficient memory to hold the object of data type int and returns a pointer to its starting point. The pointer variable holds the address of memory space allocated.
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.
Syntax:
delete pointer_variable;
Example:
Delete p;