Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does delete [] deallocate memory allocated by pointers to pointers

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?

like image 847
404compilernotfound Avatar asked Mar 02 '26 10:03

404compilernotfound


1 Answers

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;
like image 187
fredoverflow Avatar answered Mar 04 '26 22:03

fredoverflow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!