Sign in to follow this  
B_old

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

Recommended Posts

B_old    689
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?

Share this post


Link to post
Share on other sites
Evil Steve    2017
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);
}

Share this post


Link to post
Share on other sites
Marco81    136
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.

Share this post


Link to post
Share on other sites
B_old    689
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.

Share this post


Link to post
Share on other sites
frob    44975
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.

Share this post


Link to post
Share on other sites
SiCrane    11839
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().

Share this post


Link to post
Share on other sites
cache_hit    614
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 :)

Share this post


Link to post
Share on other sites
gekko    478
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]

Share this post


Link to post
Share on other sites
frob    44975
Quote:
Original post by cache_hit
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 :)
Agreed.

There were the 2005 and 2008 versions, now the 2010 version. VC 2003 is in the dustbin of history. [grin]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this