Recall & Review
beginner
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:<br>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:<br>int* arr = new int[10]; delete[] arr;<br>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++?✗ Incorrect
The
delete operator is used to free memory allocated by new in C++.How do you correctly delete an array allocated with
new[]?✗ Incorrect
Use
delete[] pointer; to properly delete arrays allocated with new[].What is the risk of deleting a pointer twice?
✗ 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?
✗ 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?✗ 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.