Complete the code to correctly delete the dynamically allocated integer.
#include <iostream> int main() { int* ptr = new int(10); [1] ptr; return 0; }
The delete operator is used to free memory allocated with new in C++.
Complete the code to delete a dynamically allocated array.
#include <iostream> int main() { int* arr = new int[5]; [1] arr; return 0; }
When deleting arrays allocated with new[], use delete[] to properly free all elements.
Fix the error in the code by choosing the correct delete syntax.
#include <iostream> int main() { double* data = new double(3.14); [1] data; return 0; }
Since data points to a single double, use delete without brackets.
Fill both blanks to correctly delete a dynamically allocated array and avoid memory leaks.
#include <iostream> int main() { char* letters = new char[10]; [1] [2]; return 0; }
Use delete[] followed by the pointer variable to free the array memory.
Fill all three blanks to correctly delete a dynamically allocated object and set the pointer to nullptr.
#include <iostream> int main() { float* value = new float(5.5f); [1] [2]; [3] = nullptr; return 0; }
Delete the object with delete, then set the pointer value to nullptr to avoid dangling pointers.