0
0
C++programming~5 mins

delete operator in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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++?
Aremove
Bfree
Cdelete
Ddispose
How do you correctly delete an array allocated with new[]?
Adelete pointer[];
Bdelete pointer;
Cfree(pointer);
Ddelete[] pointer;
What is the risk of deleting a pointer twice?
AUndefined behavior and possible crash
BNo risk, it's safe
CMemory leak
DCompiler error
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
What happens if you use delete on a pointer not allocated with new?
AUndefined behavior
BCompiler warning only
CMemory is freed safely
DNothing happens
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.