The delete operator frees memory that was previously allocated with new. It helps keep your program from using too much memory.
delete operator in C++
Introduction
Syntax
C++
delete pointer; delete[] pointer_to_array;
Use delete for single objects allocated with new.
Use delete[] for arrays allocated with new[].
Examples
C++
int* p = new int; // use p delete p;
C++
int* arr = new int[5]; // use arr delete[] arr;
Sample Program
This program creates a single integer and an array on the heap, prints their values, and then frees the memory using delete and delete[].
C++
#include <iostream> int main() { int* number = new int(42); std::cout << "Number: " << *number << std::endl; delete number; // free memory int* array = new int[3]{1, 2, 3}; std::cout << "Array elements: "; for (int i = 0; i < 3; ++i) { std::cout << array[i] << " "; } std::cout << std::endl; delete[] array; // free array memory return 0; }
Important Notes
Always match new with delete and new[] with delete[] to avoid undefined behavior.
After deleting, set pointers to nullptr to avoid dangling pointers.
Using delete on memory not allocated with new causes errors.
Summary
The delete operator frees memory allocated with new.
Use delete for single objects and delete[] for arrays.
Proper use prevents memory leaks and keeps programs efficient.
Practice
1. What is the main purpose of the
delete operator in C++?easy
Solution
Step 1: Understand memory allocation
Thenewoperator allocates memory dynamically on the heap.Step 2: Role of
Thedeletedeleteoperator frees that allocated memory to avoid leaks.Final Answer:
To free memory allocated withnew-> Option BQuick Check:
delete frees memory [OK]
Hint: Remember: new allocates, delete frees memory [OK]
Common Mistakes:
- Confusing delete with new
- Using delete to copy objects
- Thinking delete declares variables
2. Which of the following is the correct syntax to delete a dynamically allocated array
arr?easy
Solution
Step 1: Identify array deletion syntax
For arrays allocated withnew[], usedelete[]to free all elements properly.Step 2: Match correct syntax
delete[] arr;correctly deletes the entire array.Final Answer:
delete[] arr; -> Option DQuick Check:
Use delete[] for arrays [OK]
Hint: Use delete[] for arrays, delete for single objects [OK]
Common Mistakes:
- Using delete instead of delete[] for arrays
- Placing brackets after variable name incorrectly
- Writing delete array arr (invalid syntax)
3. What will be the output of the following code?
int* p = new int(10); std::cout << *p << std::endl; delete p; // std::cout << *p << std::endl; // Uncommenting this line causes what?
medium
Solution
Step 1: Analyze output before delete
The pointerppoints to an int with value 10, so*pprints 10.Step 2: Effect of delete and accessing pointer
Afterdelete p;, the memory is freed. Accessing*pafter delete causes undefined behavior (may crash or print garbage).Final Answer:
10 and then undefined behavior -> Option CQuick Check:
Access after delete = undefined behavior [OK]
Hint: Don't use pointer after delete; it's unsafe [OK]
Common Mistakes:
- Assuming deleted pointer still holds valid data
- Expecting compilation error on delete
- Ignoring undefined behavior risks
4. Identify the error in this code snippet:
int* arr = new int[5]; delete arr;
medium
Solution
Step 1: Check allocation type
arris allocated withnew int[5], which creates an array.Step 2: Correct deletion method
Arrays must be deleted withdelete[] arr;to properly free all elements.Final Answer:
Should use delete[] instead of delete -> Option AQuick Check:
Array delete needs delete[] [OK]
Hint: Match new[] with delete[] always [OK]
Common Mistakes:
- Using delete for arrays causes undefined behavior
- Thinking delete works for both single and arrays
- Ignoring memory leaks from wrong delete
5. Consider this code:
int** matrix = new int*[3];
for (int i = 0; i < 3; ++i) {
matrix[i] = new int[4];
}
// Code to delete matrix properly?
Which is the correct way to delete all allocated memory?hard
Solution
Step 1: Understand allocation
Eachmatrix[i]is an array allocated withnew int[4], so must be deleted withdelete[].Step 2: Delete outer array
Thematrixpointer itself is an array of pointers allocated withnew int*[3], so delete withdelete[] matrix;.Final Answer:
for (int i = 0; i < 3; ++i) delete[] matrix[i]; delete[] matrix; -> Option AQuick Check:
Match new[] with delete[] at all levels [OK]
Hint: Delete inner arrays with delete[], then outer with delete[] [OK]
Common Mistakes:
- Using delete instead of delete[] for inner arrays
- Deleting outer array with delete instead of delete[]
- Forgetting to delete inner arrays causing leaks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
