49

If I use .reserve(items) on a vector, the vector will allocate enough memory for my guess of the number of items that I'll need.

If I later on use .clear(), will that just clear the vector or save my earlier defined reserve?

thanks.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
monkeyking
  • 6,670
  • 24
  • 61
  • 81
  • That's a dupe of http://stackoverflow.com/questions/586634/ – sbi Apr 13 '10 at 09:35
  • 11
    No it isn't, that question doesn't even _mention_ `clear()` . The only place where the word "clear" is used is in a comment that starts with "It's not clear to me if ..." – MSalters Apr 13 '10 at 11:43
  • 1
    Possible duplicate of [What does the standard say about how calling clear on a vector changes the capacity?](http://stackoverflow.com/questions/18467624/what-does-the-standard-say-about-how-calling-clear-on-a-vector-changes-the-capac) – Jonathan Mee Jun 21 '16 at 12:17

5 Answers5

55

It is specified that std::vector<T>::clear() affects the size. It might not affect the capacity. For resetting the capacity, use the swap trick:

    std::vector<int> v1;

    // somehow increase capacity

    std::vector<int>().swap(v1);

Note: Since this old answer is still getting upvotes (thus people read it), I feel the need to add that C++11 has added std::vector<...>::shrink_to_fit(), which requests the vector to remove unused capacity.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • 1
    It's so well-known you can even find it here: http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Clear-and-minimize not the similar "Shrink to fit" for reducing the capacity of the vector to the actual number of elements (when there are some). – Matthieu M. Apr 13 '10 at 13:03
  • 2
    It _may_ affect the capacity. It is _not_ required by the Standard to avoid changing the capacity or reallocating. So there's no guarantee either way. – underscore_d Feb 18 '16 at 14:47
  • @underscore_d: Thanks. I changed the answer to fix this. – sbi Feb 18 '16 at 19:04
13

It will probably not release the reserved memory although I don't think the behaviour is specified in the standard.

EDIT: Ok, just checked and the standard only says that the post-condition is that size() == 0 although I haven't come across a vector implementation that doesn't hold on to the reserved memory.

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
  • 6
    +1 for checking the standard and "probably" (I'd normally expect the memory to remain). On an embedded system I worked with I actually HAVE come across an implementation that releases the memory on a clear. – Mark B Apr 13 '10 at 13:43
12

No it won't. Try it out by calling vector::capacity().

Further evidence of this is the appearance of shrink_to_fit. The standard's working draft mentions:

Remarks: shrink_to_fit is a non-binding request to reduce capacity() to size(). [ Note: The request is non-binding to allow latitude for implementation-specific optimizations. —end note ]

JRL
  • 76,767
  • 18
  • 98
  • 146
1

No, it won't set reserve() to 0. Calling clear() calls the destructors of each element and removes them from the vector, leaving the container with size of 0, but the capacity remains unchanged.

jasonline
  • 8,646
  • 19
  • 59
  • 80
  • _if your implementation leaves it unchanged._ the Standard does not require this, so capacity and allocation are allowed to change on `clear()`. – underscore_d Feb 18 '16 at 14:46
0

It will not affect the underlying buffer size. Which is why you have to use tricks like this to actually get rid of the buffer or make it smaller.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979