The delete keyword replaces the free function in C and will release storage reserved with new.
int *ptr1; // Declare a pointer to int.
ptr1 = new int; // Reserve storage and point to it.
float *ptr2 = new float; // Do it all in one statement.
delete ptr1; // Free the storage.
delete ptr2;
struct House // Declare a complex structure.
{
int Floors;
int Windows;
};
House *ptr3 = new House; // Reserve storage and point to it.
delete ptr3;
|
Blocks or arrays of storage can also be freed as shown in the next example.
char *ptr
ptr = new char[80];
delete [] ptr; // Free the storage.
|
Example program.
new keyword.
malloc function.
free function.
| Top | Master Index | C++ Keywords | Functions |