Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::vector<T>::clear() call destructor of the content? [duplicate]

Tags:

If I have a std::vector of something (say,does std::vector::clear() call destructor of the content? For example, if I have a data as vector, is the following code valid?

char * pd; for(vector<char *>::iterator it = data.begin(); it != data.end(); ++it) {   pd = *it;   delete pd; } 

Or, is it redundant as data.clear() would do that? I presume not, i.e. manual deallocation of the content is needed. I presume, data.clear() will not call implicit destructor for the contents? Is it correct? This is for pre C++11.

like image 885
Dr. Debasish Jana Avatar asked Mar 28 '14 09:03

Dr. Debasish Jana


People also ask

Does vector clear call destructor?

Yes a clear should call destructors on object in the vector.

What does vector Clear () do?

vector::clear() clear() function is used to remove all the elements of the vector container, thus making it size 0.

Does std::vector clear call Delete?

So what your question should be: "Does std::vector<T*>::clear() call delete on its elements?" and here the answer is: No, it does not call delete . You have to do it manually like shown in your example code, or you can (and probably should) use objects like std::string or smart pointers instead of raw pointers.

Does vector pop call destructor?

pop_back() will call the destructor of whatever's contained in the vector. In this case, it calls the destructor of a pointer -- which does absolutely nothing! You need to explicitly destroy the objects pointed at by the elements of your vector, as you did in your first code sample.

Does vector clear () call the destructor of a pointer?

std::vector clear() does call the destructor of each element in the vector: But in your case it won't delete the contents of your allocated dinamically memory. @Mhd.Tahawi: it does call the destructor for pointers. It's just that the destructor of a pointer is a trivial destructor that does nothing.

Does the C++ destructor call delete?

and here the answer is: No, it does not call delete. You have to do it manually like shown in your example code, or you can (and probably should) use objects like std::string or smart pointers instead of raw pointers. Those do the memory management for you, and their destructors (which get called, see above) call the delete for you.

Does vector clear () call delete on its elements in C++?

So what your question should be: "Does std::vector<T*>::clear () call delete on its elements?" and here the answer is: No, it does not call delete. You have to do it manually like shown in your example code, or you can (and probably should) use objects like std::string or smart pointers instead of raw pointers.

Does a vector have a destructor?

As you said correctly yourself, vector does call destructors for its elements. So, in your example the vector does call "destructors of pointers". However, you have to keep in mind that pointer types have no destructors.


2 Answers

To answer your title: std::vector<T>::clear() does call the destructor for each element. However, for primitive datatypes like char* the destructor is trivial (standardese for "does nothing") which is practically the same as if no destructor is called.

Note: A destructor is not the same as the delete operator.

So what your question should be: "Does std::vector<T*>::clear() call delete on its elements?" and here the answer is: No, it does not call delete. You have to do it manually like shown in your example code, or you can (and probably should) use objects like std::string or smart pointers instead of raw pointers. Those do the memory management for you, and their destructors (which get called, see above) call the delete for you.

like image 87
Arne Mertz Avatar answered Oct 20 '22 01:10

Arne Mertz


If your items are dynamically allocated and your vector contains pointers to them, you will have to manually call delete on each pointer in that container before calling clear(). Otherwise you have a memory leak.

If they are objects that are not dynamically allocated then you do not need to do anything - the destructor (if there is one) of the objects in the vector will be called when the vector is cleared.

like image 35
mathematician1975 Avatar answered Oct 20 '22 01:10

mathematician1975