C++ delete Keyword
Example
Free memory that was reserved with new and new[]:
int* ptr = new int;
int* myNumbers = new int[3];
*ptr = 35;
cout << *ptr;
delete ptr; // For one value
delete[] myNumbers; // For an array
Definition and Usage
The delete keyword frees dynamic memory that was reserved with new. Then the program can use the memory again. Do not use the pointer after delete, because the memory does not belong to it anymore.
Use delete for a single value, and delete[] for an array that was created with new[]. If you forget to free memory, the program uses more and more memory. This is called a memory leak.
The keyword has a second use: = delete after a function declaration means that the function must not be used. Calling it gives an error.
Related Pages
Read more about dynamic memory in our C++ new and delete Tutorial.