What is the purpose of the delete operator in C++?
The delete operator frees memory that was previously allocated with new. It helps prevent memory leaks by releasing unused memory back to the system.
Click to reveal answer
beginner
How do you use delete to free a single object allocated with new?
Use delete pointer; where pointer points to the object allocated with new. For example:
int* p = new int(5);
delete p;
Click to reveal answer
beginner
How do you use delete to free an array allocated with new[]?
Use delete[] pointer; to free memory allocated with new[]. For example:
int* arr = new int[10];
delete[] arr;
This ensures all elements are properly destroyed.
Click to reveal answer
intermediate
What happens if you use delete on a pointer that was not allocated with new?
Using delete on memory not allocated by new causes undefined behavior. This can crash the program or corrupt memory. Always delete only what you allocated with new.
Click to reveal answer
intermediate
Why should you set a pointer to nullptr after deleting it?
Setting a pointer to nullptr after deleting prevents accidental use of a dangling pointer, which points to freed memory. This helps avoid bugs and crashes.
Click to reveal answer
Which operator is used to free memory allocated by new in C++?
Aremove
Bfree
Cdelete
Ddispose
✗ Incorrect
The delete operator is used to free memory allocated by new in C++.
How do you correctly delete an array allocated with new[]?
Adelete pointer[];
Bdelete pointer;
Cfree(pointer);
Ddelete[] pointer;
✗ Incorrect
Use delete[] pointer; to properly delete arrays allocated with new[].
What is the risk of deleting a pointer twice?
AUndefined behavior and possible crash
BNo risk, it's safe
CMemory leak
DCompiler error
✗ Incorrect
Deleting a pointer twice causes undefined behavior and can crash the program.
What should you do after deleting a pointer to avoid dangling pointers?
ACall delete again
BSet it to nullptr
CDo nothing
DAssign it a new value
✗ Incorrect
Setting a pointer to nullptr after deleting prevents accidental use of invalid memory.
What happens if you use delete on a pointer not allocated with new?
AUndefined behavior
BCompiler warning only
CMemory is freed safely
DNothing happens
✗ Incorrect
Using delete on memory not allocated by new causes undefined behavior.
Explain how and why to use the delete operator in C++.
Think about memory management and avoiding bugs.
You got /5 concepts.
What are the risks of incorrect use of the delete operator?
Consider what happens if you delete wrong or forget to delete.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the delete operator in C++?
easy
A. To allocate new memory
B. To free memory allocated with new
C. To copy objects
D. To declare variables
Solution
Step 1: Understand memory allocation
The new operator allocates memory dynamically on the heap.
Step 2: Role of delete
The delete operator frees that allocated memory to avoid leaks.
Final Answer:
To free memory allocated with new -> Option B
Quick 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
A. delete arr;
B. delete array arr;
C. delete arr[];
D. delete[] arr;
Solution
Step 1: Identify array deletion syntax
For arrays allocated with new[], use delete[] to free all elements properly.
Step 2: Match correct syntax
delete[] arr; correctly deletes the entire array.
Final Answer:
delete[] arr; -> Option D
Quick 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
A. 10 and then 10 again
B. Compilation error at delete
C. 10 and then undefined behavior
D. Runtime error at new
Solution
Step 1: Analyze output before delete
The pointer p points to an int with value 10, so *p prints 10.
Step 2: Effect of delete and accessing pointer
After delete p;, the memory is freed. Accessing *p after delete causes undefined behavior (may crash or print garbage).
Final Answer:
10 and then undefined behavior -> Option C
Quick 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
A. Should use delete[] instead of delete
B. Should use delete without brackets
C. No error, code is correct
D. Should not delete dynamically allocated arrays
Solution
Step 1: Check allocation type
arr is allocated with new int[5], which creates an array.
Step 2: Correct deletion method
Arrays must be deleted with delete[] arr; to properly free all elements.
Final Answer:
Should use delete[] instead of delete -> Option A
Quick 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
A. for (int i = 0; i < 3; ++i) delete[] matrix[i]; delete[] matrix;
B. for (int i = 0; i < 3; ++i) delete matrix[i]; delete[] matrix;
C. delete[] matrix; delete[] matrix[0];
D. delete matrix; delete matrix[0];
Solution
Step 1: Understand allocation
Each matrix[i] is an array allocated with new int[4], so must be deleted with delete[].
Step 2: Delete outer array
The matrix pointer itself is an array of pointers allocated with new int*[3], so delete with delete[] matrix;.
Final Answer:
for (int i = 0; i < 3; ++i) delete[] matrix[i]; delete[] matrix; -> Option A
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.