3

I have an array of pointers (that I created by calling new ptr*[size]). All of these pointers point to an object that was also put on the heap.

What is the proper way to delete the array and all new'd ptr's?

This is what I do now:

for (int i = 0; i < size; i++) delete array[i];
delete[] array; // Not sure since this double deletes array[0]

Does this do what I think it should?

Thanks

3
  • 1
    What is ptr? This matters. Commented Dec 18, 2012 at 23:38
  • 1
    The correct way to do this is not to do this at all. Prefer, perhaps, std::vector<std::unique_ptr<T>>, which requires no manual cleanup at all. Commented Dec 18, 2012 at 23:40
  • let's say can be replaced with int here Commented Dec 18, 2012 at 23:42

4 Answers 4

12

Every pointer allocated with new gets a corresponding delete. Every pointer allocated with new [] gets a corresponding delete []. That's really all you need to know. Of course, when you have a dynamically allocated array which contains dynamically allocated pointers the deallocation must occur in reverse order.

So it follows that the correct idiom would be...

int main()
{
    int **container = new int*[n];
    for(int i = 0; i < n; ++i)
        container[i] = new int[size];

    // ... and to deallocate...
    for(int i = 0; i < n; ++i)
        delete [] container[i];

    delete [] container;
}

And then of course I say "stop doing that" and recommend you use a std::array or std::vector (and the template type would be unique_ptr<int>).

Sign up to request clarification or add additional context in comments.

6 Comments

(+1) whole answer, but, a plus for "reverse order". In theory, It doesn't matter the allocation/deallocation order, but, I agree that its more efficient, to deallocate in reverse order ...
@umlcat: Deallocation order does matter though. In this example the expression container[n] would result in undefined behavior if delete [] container had already been called.
The part that I don't get is how 'delete [] container[0]' and 'delete [] container' not break since we are deleting with a pointer to the same thing
@jamesatha: container[0] is not the same thing as container. I think you are confused because container is a pointer to the first element and so is &container[0], but container[0] is actually the first element. Not the same.
@EdS. Thanks, I forgot that!
|
2

Yes, that does what you think it should. Since you did new for each element, you have to delete each element. And since you did new[] for the entire array, you need to delete[] the entire array.

As @djechlin rightly says in the comments, there's not really enough information to go on, but I'm presuming your prior code is something like this:

int** array = new int*[5];
for (int i = 0; i < 5; i++) {
  array[i] = new int;
}

Note that array is not actually an array type. It is a "pointer to pointer to int" and the array of pointers it points to was allocated with new[]. That's why you need to delete[] it.

7 Comments

This is not clear from OP's question - don't know how ptr is allocated, and I only see one new in his code, should be only one delete.
but doesn't delete array[0] and delete array point to the same thing?
@djechlin "All of these pointers point to an object that was also put on the heap."
Put on the heap in a constructor and removed in a destructor possibly? Type of ptr matters.
@djechlin destructors are called throught delete, aren't they?
|
1

Yes. First you have to free the object each pointer in the array points to, then you have to free the array itself. In that order. If you reverse the order you'll have no reference to the objects and will leak a lot of memory.

Comments

0

Yes, first you delete each object to which elements of array point, and then you delete array of pointers itself. If you want to check your memory management, you can use tools like valgrind, they will be able to spot most errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.