views:

184

answers:

5

Hello

I have allocated and array of Objects

Objects *array = new Objects[N];

How should I delete this array? Just

delete[] array;

or with iterating over the array's elements?

for(int i=0;i<N;i++)
    delete array[i];
delete[];

Thanks

UPDATE:

I changed loop body as

delete &array[i];

to force the code to compile.

+1  A: 
delete [] array

is enough.

vava
+2  A: 

Just delete[] array is sufficient. It is guaranteed that each element of the array is deleted when you delete an array using delete[] operator.

Naveen
+4  A: 

As a general rule you should delete/delete[] exactly those things that you allocated with new/new[]. In this case you have one allocation with new[], so you should use one call to delete[] to free that allocated thing again.

That the deletes in the for-loop won't compile is also a good indication that they are not the right way to do it.

sth
+2  A: 

Not only is

delete [] array;

enough, but if you do

for(int i=0;i<N;i++)
    delete &array[i];
delete[] array;

you'll be causing undefined behavior, because

delete &array[i];

will be deleting things that weren't returned by a new operation.

Not to mention that the subsequent delete[] array; will call the destructor for all the objects that just had destructors called in the loop.

So don't do that.

Michael Burr
+3  A: 

Every use of new should be balanced by a delete, and every use of new[] should be balanced by delete[].

for(int i=0;i<N;i++)
    delete array[i];
delete[] array;

That would be appropriate only if you initialized the array as:

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

That fact that your original code gave you a compilation error is a strong hint that you're doing something wrong.

BTW, obligatory: avoid allocating arrays with new[]; use std::vector instead, and then its destructor will take care of cleanup for you.

jamesdlin
I can't use vector. It is a homework :)
osgx
No! Don't use std::vector blindly as a substitute for arrays! That's like saying "I have a transportation need, so I'll use a car." Sometimes it's the right answer, but sometimes you need to go next door, and sometimes you need to go from Montreal to Moscow. In many cases where an array is close to the right answer but you want a standard-library, std::valarray is what you want. And in many cases an array is, in fact, the right answer.
Brooks Moses
@Brooks. I can think of any situation where a dynamically allocated array can not be replaced efficiently with a std::vector. This is because a std::vector __IS__ an array (the only difference is that the vector handles all the memory management). If it is a statically allocated array fine you may get a tiny performance improvement with just using an array.
Martin York
Technically, `std::vector` is **not** an array, but a class (why do you say that it is an array, but then say "the only difference is"?). But non-technically (speaking loyally) it is an array because the usage is similarly.
Johannes Schaub - litb