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.
Yes a clear should call destructors on object in the vector.
vector::clear() clear() function is used to remove all the elements of the vector container, thus making it size 0.
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With