How to Properly Delete a Pointer to Array

how to properly delete a pointer to array

The requirement to match new[] with delete[] is technically correct.

Much better, however (at least in my opinion), would be to forget that you ever even heard of new[], and never use it again. I'm pretty sure it's been (at least) 10 years since the last time I used new[], and if I'd understood the situation very well, I'd have stopped even sooner than that. Pretty nearly any time you'd even consider using new[], an std::vector (or possibly std::deque) will be a better choice.

If you're trying to create something roughly equivalent to a vector or deque yourself, you don't normally want to use new[] for that either. They way they (at least normally, though it's possible to change this via a custom Allocator class) is to allocate "raw" storage with operator new (which is pretty much like malloc--you just give it a size, and it gives you that many bytes of storage). Then you use the placement new operator to create objects in that space, and explicitly invoke the object's destructor to destroy objects in that space.

To give one example, this is what allows std::vector to support reserve, which allows you to allocate extra space, which won't be turned into objects until you call something like push_back or emplace_back to create an actual object in the space you allocated.

When you use new[], it has to create objects of the specified type filling all the space you allocate. You can't create something like push_back that adds a new object to the collection, because the collection is always already "full". All you can do is allocate a new collection that's larger than the old one (so every addition to the collection is O(N) instead of the amortized O(1) supported by std::vector--a huge loss of efficiency).

C++ Array of pointers: delete or delete []?

delete[] monsters;

Is incorrect because monsters isn't a pointer to a dynamically allocated array, it is an array of pointers. As a class member it will be destroyed automatically when the class instance is destroyed.

Your other implementation is the correct one as the pointers in the array do point to dynamically allocated Monster objects.

Note that with your current memory allocation strategy you probably want to declare your own copy constructor and copy-assignment operator so that unintentional copying doesn't cause double deletes. (If you you want to prevent copying you could declare them as private and not actually implement them.)

C++: How to properly delete array of pointers to pointers in class destructor

In all of the similar questions I found, each element of the array is assigned a value dynamically, using new as such: arr[i] = new First(); . However, here the elements are assigned the value of a pointer to an object that is a parameter of the function. So, should the destructor delete every element one by one and then delete the array, or is it enough to delete the array?

That, we cannot answer. Does Second take ownership of the objects passed to .add(), and if so how were they allocated?

  1. If it does not take ownership, just deleting the array is enough, and that array should be managed by a std::unique_ptr doing so for you.

  2. If it does take ownership, that argument to .add() should be the smart-pointer with the right ownership-semantics and deleter. Your array should then be an array of those smart-pointers, managed by a std::unique_ptr.

In either case, if you properly use smart-pointers, the default-dtor is fine.

Delete a pointer to pointer (as array of arrays)

Simple rules to follow:

  • for each allocation, there has to be a deallocation (ex1 is therefore wrong)
  • what was allocated using new should be freed using delete, using new[] should be deallocated using delete[] and using malloc should be deallocated using free (ex3 is therefore wrong)

Conclusion, ex2 is OK.

Proper way to delete an array of pointers

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>).

How can I delete an object from a pointer to a pointer array?

If you're reassigning a member of the array to point to a new object, you can first deallocate the old object, if any.

Input* oldInput = this->Inputs[inputNumber];
delete oldInput;

this->Inputs[inputNumber] = new DigitalInput(params...)

pointer to array of pointers! how to delete?

No problem. It's right!
You deallocated in the reverse order that you allocated!
I don't even think there's another way to do that.

Delete a pointer array without deleting the pointed objects in memory?

What you describe does not sound right.

Let's say you have a class A and you create an array of As with:

A** array1 = new A*[32];

Then fill it:

for(int i = 0; i < 32; ++i)
array1[i] = new A();

Doing a delete[] array1 does not free the elements of array1.

So this is safe:

A** array1 = new A*[32];
for(int i = 0; i < 32; ++i)
array1[i] = new A();

A** arary2 = new A*[64];
for(i = 0; i < 32; ++i)
array2[i] = array1[i];

delete [] array1;

for(i = 0; i < 32; ++i)
// do something with array2[i]


Related Topics



Leave a reply



Submit