I have an array of pointers to other objects called Comparable* array (inside a template for a class).
I understand that delete deletes memory referenced by a pointer, and that delete [] deallocates the memory assigned to each pointer in an array.
My question is if I have an array that contains pointers to other objects, how do I deallocate the memory referenced by each pointer in the array and the array itself?
if I have an array that contains pointers to other objects, how do I deallocate the memory referenced by each pointer in the array AND the array itself?
The way you just described :) Loop through the array to delete every object and then delete the array:
for (int i = 0; i < n; ++i)
delete array[i];
delete[] array;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With