std::vector setting size == 0 but keeping capacity

Started by
10 comments, last by frob 14 years, 1 month ago
Hi, in one loop I'm filling a std::vector with push_back(). I then call clear() so that I can use the vector in another loop that also fills the vector with push_back(). Is there a a way to achieve this without de-allocating the previous data?
Advertisement
Quote:Original post by B_old
Hi,

in one loop I'm filling a std::vector with push_back(). I then call clear() so that I can use the vector in another loop that also fills the vector with push_back().
Is there a a way to achieve this without de-allocating the previous data?
I don't follow - what behaviour to you want, and what behaviour do you see? Currently, the vectors capacity will remain unchanged by the clear() call. If you want to change its capacity to zero, to free the internal memory, you'll need to use the "swap trick":
template<typename T> void clear_vector(std::vector<T>& _vec){   std::vector<T> tmp;   tmp.swap(_vec);}
If you know how much elements are going to be put in the vector, you can call std::vector::resize(count) to allocate the elements before the loop. Inside the loop you can use the [] operator to assign the elements instead of using push_back(). After the first loop the resize(count) function won't deallocate the first count elements, they will be overwritten inside the 2nd for loop.
Quote:Original post by Evil Steve
I don't follow - what behaviour to you want, and what behaviour do you see? Currently, the vectors capacity will remain unchanged by the clear() call. If you want to change its capacity to zero, to free the internal memory, you'll need to use the "swap trick":
template<typename T> void clear_vector(std::vector<T>& _vec){   std::vector<T> tmp;   tmp.swap(_vec);}

Hm. Seems like I was confused. I wanted the capacity not to be zero, which is also what I got as it turns out.
Specifically I don't want to allocate memory in the second loop if I already had enough memory in the first loop.

All the major implementations keep memory around after removing items. This isn't a requirement, but something that everybody does because reallocation are expensive.


See GOTW #54 part 2 for some extra details on it, including how to get the memory released if you want to.
^ Oh nice Frob. I didn't know that.
Quote:Original post by frob
All the major implementations keep memory around after removing items.

This depends on your definition of major implementation. MSVC 2003's vector implementation deallocates the entire buffer when you call clear().
Quote:Original post by SiCrane
Quote:Original post by frob
All the major implementations keep memory around after removing items.

This depends on your definition of major implementation. MSVC 2003's vector implementation deallocates the entire buffer when you call clear().


I don't consider 2003 a major implementation :)
I might be missing something, but won't a simple resize(0) rather than clear() do what the OP wants?
Use resize(0).

Edit: I've been under the impression for years resize(0) was required to keep the capacity, but now I can't find where I read it. Can't tell if the standard allows erase to change the capacity or not. So now I'm second-guessing my reply.

[Edited by - gekko on March 18, 2010 11:23:09 AM]
-- gekko

This topic is closed to new replies.

Advertisement