delete operator in C++ - Time & Space Complexity
When using the delete operator in C++, it's important to understand how its execution time grows as the program runs.
We want to know how the cost of deleting memory changes with the size or number of objects involved.
Analyze the time complexity of the following code snippet.
int* arr = new int[100];
// ... use the array ...
delete[] arr;
int* single = new int;
// ... use the single int ...
delete single;
This code allocates memory for an array and a single integer, then frees that memory using delete[] and delete.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
delete[]anddeleteoperators deallocate the memory blocks. - How many times: Each runs once, regardless of the array size.
Both the delete[] for the array and delete for a single object run once, so they are constant time.
As the number of elements in the array grows, the time to delete remains constant for primitive types.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 deallocate operation |
| 100 | 1 deallocate operation |
| 1000 | 1 deallocate operation |
Pattern observation: The time to delete an array is constant, independent of its size (for primitive types).
Time Complexity: O(1)
This means deleting an array or a single object takes constant time (for primitive types like int).
[X] Wrong: "Deleting an array always takes O(n) time proportional to its size."
[OK] Correct: For primitive types like int, delete[] deallocates the entire memory block in constant time, as there are no non-trivial destructors to call for each element.
Understanding how delete works helps you write efficient code and avoid surprises in performance when managing memory.
"What if we used smart pointers instead of raw pointers? How would the time complexity of deleting change?"
Practice
delete operator in C++?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]
- Confusing delete with new
- Using delete to copy objects
- Thinking delete declares variables
arr?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]
- Using delete instead of delete[] for arrays
- Placing brackets after variable name incorrectly
- Writing delete array arr (invalid syntax)
int* p = new int(10); std::cout << *p << std::endl; delete p; // std::cout << *p << std::endl; // Uncommenting this line causes what?
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]
- Assuming deleted pointer still holds valid data
- Expecting compilation error on delete
- Ignoring undefined behavior risks
int* arr = new int[5]; delete arr;
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]
- Using delete for arrays causes undefined behavior
- Thinking delete works for both single and arrays
- Ignoring memory leaks from wrong delete
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?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]
- Using delete instead of delete[] for inner arrays
- Deleting outer array with delete instead of delete[]
- Forgetting to delete inner arrays causing leaks
Ready to prove it?
Pass the test and this pattern moves unknown
