As I read in the reference page for std::vector clear function()
" All the elements of the vector are dropped: their destructors are called, and then they are removed from the vector container, leaving the container with a size of 0."
It saids that it would call all the of destructors of the erasing elements in side that vector when clear() is called. Just wondering if this is true? Because I went through with the GDB debugger it doesn't seem to call the destructors for them.
Here a test code that I wrote just wondering will this give me memory leak if delete is not called.
Again, it does call the destructors.
In your example, you should simply std::vector<int> vect;
If you need to use pointers because you want a polymorphic container, I know two possibilities:
_ Use smart_pointers. As STL containers use copy mechanism (c++98) you will need shared_ptr
_ Especialize a container to handle pointers. By instance boost::ptr_vector. I guess that you could simply modify the allocator used, making the destruct() method responsible for the delete, but I haven't tested that.
void some_function()
{
std::vector<int*> vec1;
std::vector<int*> vec2;
//assume that vec1 is holding a few int* elements and vec2 is empty.
//when I do
vec2 = vec1;
// vec2 now only contain pointers of int not the actualy object.
// manually erase all objects in vector instead of calling vec1.clear()
for(std::vector<int*>::iterator iter = vec1.begin();iter != vec.end();iter++)
{
int *temp = *iter;
delete temp;
temp = NULL;
}
now vec2.at(0) would contain garbage
}
So I guess my question is what is a proper to copy std::vector<int*> thanks. or I have to manually do it like what I did with delete. thank-you