Why clear() used on vector of vectors doesn't clear the values of those vectors?

I was so sure that vector calls the destructors of the contained objects that I just used the clear() on the main vector and then I was shocked when I reused the vector and the values of the vectors stored in it were not cleared...

EDIT - code:

vector<vector<int> > flush1;
flush1.reserve(4);
for(int i = 0; i != 4; ++i) flush1[i].reserve(7);
flush1.clear();
flush1[0].push_back(some_int);
flush1[1].push_back(some_int);
flush1[2].push_back(some_int);
flush1[3].push_back(some_int);
cout the size from flush1[0-3];
flush1.clear();

and again

flush1[0].push_back(some_int);
flush1[1].push_back(some_int);
flush1[2].push_back(some_int);
flush1[3].push_back(some_int);

and cout-ing the size from flush1[0-3] cout-s their previous size + new sizes (addition of old and new)

Answers


The vector object has three pointers: the beginning, the end and end-of-reserved-storage.

The clear() method calls the destructor on each contained object and then sets the end pointer to the beginning.

So the data of the destructed objects will still be visible (using a debugger for example) in the reserved storage.


Need Your Help

WCF store data for current context

c# multithreading entity-framework wcf azure

I have an application existing of a website, local WPF client and a WCF service for communication with the client. These applications are currently being installed once per customer. I'd like to mi...

How to add a horizontal gap with a JLabel

java swing jlabel swingx

I have a JLabel (actually, it is a JXLabel).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.