Does [] delete call destructors?

I am writing a template class that internally manages an array of this type. Like this:

template<typename T> class Example { // ... private: T* objects; // allocated in c'tor (array), deleted in d'tor // ... }; 

I was wondering if C ++ calls the destructor of each object in objects when I delete it through delete[] objects; .

I need to know this because the objects in my class do not contain reasonable values ​​all the time, so destructors should not be called when they do not.

Also, I would like to know if destructors will be called if I declare a fixed-size array, for example T objects[100] , as part of Example<T> .

+8
c ++ delete-operator
source share
6 answers

If T has a destructor, then it will be called delete[] . From section 5.3.5 Removing the C ++ 11 standard (project n3337), clause 6:

If the operand value of the delete-expression is not a null pointer value, the delete-expression will call the destructor (if any) for the object or elements of the array to be deleted . In the case of an array, the elements will be destroyed in descending order of the address (that is, in the reverse order of completion of their constructor; see 12.6.2).

A destructor for type T will also be called for each element in the array T[] when the array is not allocated dynamically and the array goes out of scope (end of life).


I need to know this because the objects in my class do not contain reasonable values ​​all the time, so destructors should not be called when they do not.

But it seems that there is a very significant problem with the object, which can receive a state in which it cannot be destroyed.

+18
source share

Yes, the destructor will be called for all objects in the array when using delete[] . But this should not be a problem, since the constructor was called for all objects in the array when you used new[] (you did, right?) To select it.

If the constructed object can be in such a state that the call to the destructor will be invalid, then something seriously does not correspond to your object. You need your destructor to work in all cases.

+3
source share

The answer is yes. A destructor is called for each object.

In a related note, you should try to avoid using delete whenever possible. Instead, use smart pointers (e.g. unique_ptr , shared_ptr ) and STL containers (e.g. std :: vector, std :: array).

+2
source share

delete[] objects similar (but not identical):

 for (i = 0; i < num_of_objects; ++i) { delete objects[i]; } 

since delete invokes the destructor, you can expect delete[] do the same.

+1
source share

delete [] calls the destructor for each element of the array. The same thing happens for a member array (your T objects[100] ).

You want to save it as a pointer and construct the destructor (both the copy constructor and the copy assignment operator, see the three / five rule ) for your template. deal with the "insensitive" values ​​that objects points to.

+1
source share

Yes, delete[] ensures that destructors are called on every object.

Depending on your use case, using Boost pointer containers or just containers with smart pointers can make it a lot easier (exception safe) pointer set.

0
source share

All Articles