Lesson 10 | Free store operators |
Objective | C++ delete operator |
C++ delete Operator
Examine the use of the operator delete to deallocate free store memory.
The operator delete
destroys a variable created by new
, in effect returning its allocated storage to free store for re-use. The operator delete
returns type void
and has two forms.
Non-array form
When the corresponding new
expression did not allocate an array, you use this form:
delete
expression
Example
When
p = new my_type;
is executed, you use
delete p;
to deallocate the free store used by p
.
Array form
When the corresponding new
expression allocated an array, you use this form:
delete
[]
expression
Example
When
p = new my_type[size];
is executed, you use
delete []p;
to deallocate the free store used by p
.
In the next lesson, we will look at how new
and delete
are used to dynamically allocate an array.