2

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,837
  • 2
  • 18
  • 26
404compilernotfound
  • 143
  • 2
  • 6
  • 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

10

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
  • 256,549
  • 94
  • 388
  • 662
  • +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
6

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
  • 42,307
  • 41
  • 144
  • 212
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
  • 34,573
  • 20
  • 89
  • 115
0

No, it doesn't.

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

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
  • `an array of objects`? I would say *delete[] deletes a memory pointed to by pointer, allocated using new[]*. – Nawaz Sep 13 '11 at 06:01
  • @Nawaz: wrong. In addition to freeing memory, `delete[]` also calls object destructors. – Serge Wautier Sep 13 '11 at 06:18
  • @Nawaz: Variables and array elements are objects in C++. As are arrays themselves :) – fredoverflow Sep 13 '11 at 06:19
  • 1
    @Nawaz: No, `delete` and `delete[]` deal with objects, not (just) memory. When objects are deleted, their destructors need to be called. That's what `delete` and `delete[]` do. – n. m. could be an AI Sep 13 '11 at 06:21
  • @Serge: Correct. I wrote that incorrectly. I've to say it as: *delete[] deletes a pointer allocated using new[]*. – Nawaz Sep 13 '11 at 06:24