Complete the code to allocate memory for an integer pointer.
int* ptr = new [1];new for dynamic allocation.The new operator allocates memory for the specified type. Here, we want an integer pointer, so int is correct.
Complete the code to properly deallocate the dynamically allocated memory.
delete [1];delete[] for single object allocation.To free memory allocated with new, use delete followed by the pointer variable name.
Fix the error in the code that causes a memory leak by missing deallocation.
void leak() {
int* data = new int[10];
// Missing [1]
}delete instead of delete[] for arrays.When memory is allocated with new[], it must be deallocated with delete[] to avoid memory leaks.
Fill both blanks to create a loop that allocates and then deallocates memory correctly.
for (int i = 0; i < 5; i++) { int* ptr = new [1]; // use ptr [2] ptr; }
delete[] for single object allocation.Each iteration allocates an int and then deallocates it with delete to avoid memory leaks.
Fill all three blanks to create a dictionary-like structure using std::map that stores dynamically allocated integers and properly deletes them.
#include <map> #include <string> void cleanup(std::map<std::string, int*>& data) { for (auto& [1] : data) { [2] [3]; } data.clear(); }
The loop iterates over each pair in the map. The dynamically allocated integer is pointed to by pair.second, which must be deleted to avoid memory leaks.