<p>So I have a pointer to an array of pointers.  If I delete it like this:</p>

<pre><code>delete [] PointerToPointers;
</code></pre>

<p>Will that delete all the pointed to pointers as well?  If not, do I have to loop over all of the pointers and delete them as well, or is there an easier way to do it?  My google-fu doesn't seem to give me any good answers to this question.</p>

<p>(And yeah, I know I need to use a vector.  This is one of those "catch up on C++" type assignments in school.)</p>

[{'answer_id': 48095, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 1, 'selected': False, 'text': "<p>I think you're going to have to loop over I'm afraid.</p>\n"}, {'answer_id': 48098, 'author': 'Jeffrey', 'author_id': 3259, 'author_profile': 'https://Stackoverflow.com/users/3259', 'pm_score': 2, 'selected': False, 'text': '<p>Pointers are pretty much just memory references and not spiffy little self-cleaning .net objects.  Creating proper destructors for each class will make the deletion a little cleaner than massive loops throughout the code.</p>\n'}, {'answer_id': 48102, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 6, 'selected': True, 'text': '<p>Yes you have to loop over the pointers, deleting individually.</p>\n\n<p>Reason: What if other code had pointers to the objects in your array?  The C++ compiler doesn\'t know if that\'s true or not, so you have to be explicit.</p>\n\n<p>For an "easier way," two suggestions: (1) Make a subroutine for this purpose so at least you won\'t have to write the code more than once. (2) Use the "smart pointer" design paradigm where you hold an array of objects with reference-counters, then the objects are deleted when the objects are no longer referenced by any code. </p>\n'}, {'answer_id': 48670, 'author': 'Ray', 'author_id': 456786, 'author_profile': 'https://Stackoverflow.com/users/456786', 'pm_score': 4, 'selected': False, 'text': '<p>I agree with Jason Cohen though we can be a bit clearer on the reason for needing to delete your pointers with the loop. For every "new" or dynamic memory allocation there needs to be a "delete" a memory de-allocation. Some times the "delete" can be hidden, as with smartpointers but it is still there. </p>\n\n<pre><code>int main()\n{\n  int *pI = new int;\n  int *pArr = new int[10];\n</code></pre>\n\n<p>so far in the code we have allocated two chunks of dynamic memory. The first is just a general int the second is an array of ints.</p>\n\n<pre><code>  delete pI;\n  delete [] pArr;\n</code></pre>\n\n<p>these delete statements clear the memory that was allocated by the "new"s</p>\n\n<pre><code>  int ppArr = new int *[10];\n\n  for( int indx = 0; indx &lt; 10; ++indx )\n  {\n    ppArr[indx] = new int;\n  }\n</code></pre>\n\n<p>This bit of code is doing both of the previous allocations.  First we are creating space for our int in a dynamic array.  We then need to loop through and allocate an int for each spot in the array.</p>\n\n<pre><code>  for( int indx = 0; indx &lt; 10; ++indx )\n  {\n    delete ppArr[indx];\n  }\n  delete [] ppArr;\n</code></pre>\n\n<p>Note the order that I allocated this memory and then that I de-allocated it in the reverse order. This is because if we were to do the delete [] ppArr; first we would lose the array that tells us what our other pointers are. That chunk or memory would be given back to the system and so can no longer be reliably read.</p>\n\n<pre><code>  int a=0;\n  int b=1;\n  int c=2;\n\n  ppArr = new int *[3];\n\n  ppArr[0] = &amp;a;\n  ppArr[1] = &amp;b;\n  ppArr[2] = &amp;c;\n</code></pre>\n\n<p>This I think should be mentioned as well. Just because you are working with pointers does not mean that the memory those pointers point to was dynamically allocated. That is to say just because you have a pointer doesn\'t mean it necessarily needs to be delete. The array I created here is dynamically allocated but the pointers point to local instances of ints When we delete this we only need to delete the array.</p>\n\n<pre><code>  delete [] ppArr;\n\n  return 0;\n\n}\n</code></pre>\n\n<p>In the end dynamically allocated memory can be tricky and anyway you can wrap it up safely like in a smart pointer or by using stl containers rather then your own can make your life much more pleasant.</p>\n'}, {'answer_id': 59481, 'author': 'Greg Rogers', 'author_id': 5963, 'author_profile': 'https://Stackoverflow.com/users/5963', 'pm_score': 2, 'selected': False, 'text': '<p>See <a href="http://www.boost.org/doc/libs/1_36_0/libs/ptr_container/doc/ptr_container.html" rel="nofollow noreferrer">boost pointer container</a> for a container that does the automatic deletion of contained pointers for you, while maintaining a syntax very close to ordinary STL containers.</p>\n'}, {'answer_id': 752538, 'author': 'AndreasT', 'author_id': 82673, 'author_profile': 'https://Stackoverflow.com/users/82673', 'pm_score': 0, 'selected': False, 'text': "<p>I don't know why this was answered so confusingly long.</p>\n\n<p>If you delete the array of pointers, you will free\nthe memory used for an array of usually ints.<br>\na pointer to an object is an integer containing the adress.</p>\n\n<p>You deleted a bunch of adresses, but no objects.</p>\n\n<p>delete does not care about the content of a memory space,\nit calls a destructor(s) and marks the mem as free.</p>\n\n<p>It does not care that it just deleted a bunch of adresses\nof objects, it merely sees ints.</p>\n\n<p>That's why you have to cycle through the array first! and call delete\non every element, then you can delete the storage of the array itself.</p>\n\n<p>Well, now <em>my</em> answer got somewhat long... .... strange...       ;)</p>\n\n<p>Edit:\nJason's answer is not wrong, it just fails to hit the spot. Neither\nthe compiler nor anything else in c(++) cares about you deleting stuff that is elsewhere\npointed to. You can just do it. Other program parts trying to use the deleted objects\nwill segfault on you. But no one will hinder you.\nNeither will it be a problem to destroy an array of pointers to objects, when the objects\nare referenced elsewhere.</p>\n"}, {'answer_id': 3822166, 'author': 'RikSaunderson', 'author_id': 450460, 'author_profile': 'https://Stackoverflow.com/users/450460', 'pm_score': 2, 'selected': False, 'text': "<p>Let's take a (pseudocoded) real world example  .Imagine that you had a class like this:</p>\n\n<pre><code>class Street\n{\n    public:\n        Street();\n        ~Street();\n    private:\n        int HouseNumbers_[];\n}\n\ntypedef *Street StreetSign;\n</code></pre>\n\n<p>If you have an array of street signs, and you delete your array of streetsigns, that doesn't mean that you automatically delete the sreets.  They re still there, bricks and mortar, they just don't have those signs pointing to them any more.  You have got rid of <em>those specific</em> instances of pointers to the streets.</p>\n\n<p>An array of pointers is (conceptually) a bit like an array of integers, it's an array of numbers representing the memory locations of various objects.  It isn't the objects themselves.  </p>\n\n<p>If you delete[] the array of pointers, all you have done is delete an array of integers.  </p>\n"}]
['https://Stackoverflow.com/questions/48094', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2147/']