Junior — Middle
Explain what the delete[] operator means in C++ and in what cases it is used.
sobes.tech AI
Answer from AI
The delete[] operator in C++ is used to free memory allocated for an array of objects created with the new[] operator.
When you create a dynamic array:
int* arr = new int[10];
you should free the memory as follows:
delete[] arr;
This ensures that the destructor (if any) is called for each element of the array, and the memory is properly released.
Use delete[] specifically for arrays, and for single objects, just delete. Incorrect usage can lead to undefined behavior and memory leaks.