1

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?

AAEM
  • 1,768
  • 1
  • 16
  • 26
404compilernotfound
  • 134
  • 2
  • 7
  • 14
  • 3
    -1 for not posting the code. It's very common sense that code makes question *easy* to understand without having to make any *assumption*. – Nawaz Sep 13 '11 at 05:54
  • 6
    +1 although posting code is often a good idea, many developers need to learn to read plain text and not only programs. In this case, plain text is self explanatory enough. – Serge Wautier Sep 13 '11 at 05:57
  • [related answer](http://stackoverflow.com/questions/4260464/does-stdlistremove-method-call-destructor-of-each-removed-element/4261074#4261074) and [related FAQ](http://stackoverflow.com/questions/4810664/) – fredoverflow Sep 13 '11 at 06:18
  • @Serge: I've learnt to read plain text but I do that when I read post of someone who I believe knows the languages very well. When a person who comes with his doubts in C++ basics, then I really really doubt if he correctly uses the terminologies when describing his problem in *plain text*. For example, in the title he has written *pointers to pointers*, whereas in the post, he started with "an array of pointers". Is it clear enough to you? – Nawaz Sep 13 '11 at 06:20
  • 1
    @Nawaz The *pointers to pointers* part of the title doesn't make any sense, but IMO his question is clear enough. All 3 posted answers say the same thing, so obviously Serge and I are not the only ones who think so. – Praetorian Sep 13 '11 at 06:31

4 Answers4

8

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;
fredoverflow
  • 246,999
  • 92
  • 370
  • 646
  • +1, and remembering that in the case of a pointer array of pointers-to-arrays, the loop-delete should be `delete[]` as well. In short, anything `new type[n]` needs `delete[]`. Nice answer. – WhozCraig Apr 07 '13 at 21:38
5

delete[] calls destructor for every object in array, if such destructor exists. For array of pointers, delete[] does not release every pointer, since pointer is plain type without destructor. You need to delete every pointer in the code.

Alex F
  • 40,884
  • 40
  • 141
  • 206
1

You need to loop over the array to deallocate the locations referenced by array indexes and need to deallocate the array itself at the end, after the loop.

Note: Assuming you have dynamically allocated using new[]

Mahesh
  • 33,625
  • 17
  • 84
  • 113
0

No, it doesn't.

delete[] deletes an array of objects allocated using new[], not an array of pointers.

Serge Wautier
  • 20,884
  • 13
  • 66
  • 107