Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vector<T>.clear() and reallocation

27 views
Skip to first unread message

Ralf Goertz

unread,
Oct 13, 2017, 5:29:36 AM10/13/17
to
Hi,

I have a vector of type vector<char> with very many elements. Now I want
to clear it keeping its current capacity because it is going to be
filled again to about its previous size. Ist there a way to do that? At
http://www.cplusplus.com/reference/vector/vector/clear/ I read that
"reallocation is not guaranteed to happen, and the vector capacity is
not guaranteed to change due to calling this function". That means it is
also not guaranteed not to change. Can I make sure it doesn't change?

Bo Persson

unread,
Oct 13, 2017, 6:06:07 AM10/13/17
to
Yes, by using a better reference

http://en.cppreference.com/w/cpp/container/vector/clear



Bo Persson

Paavo Helde

unread,
Oct 13, 2017, 6:14:07 AM10/13/17
to
The standard does not give certain guarantees either way. However, there
would be not much point to call clear() if one does not intend to reuse
the vector, so from QOI viewpoint the capacity should remain the same
and as far as I have understood most implementations have always
followed this.

In C++11 a new function std::vector::shrink_to_fit() was added
specifically to address the concerns of people who wanted to reduce the
capacity after erase() and clear(). This also gives a strong hint that
clear() should not do that.

I would put a debug-mode assert into the code checking that the
capacity() remains the same after clear(), so I would get alerted
whenever some new compiler version does not follow the suite.





Paavo Helde

unread,
Oct 13, 2017, 11:50:20 AM10/13/17
to
This reference relies on the verbiage in the standard: "No reallocation
shall take place during insertions that happen after a call to reserve()
until the time when an insertion would make the size of the vector
greater than the value of capacity()."

However, this claim does not really hold. If there is a shrink_to_fit()
call or a swap() with a default-constructed vector between reserve() and
insert, then the vector capacity is reset (as it should). So I think the
above remark makes a silent assumption that the vector is not shrink or
swapped meanwhile "during insertions"; this silent assumption could very
reasonably involve clear() and erase() as well. Ergo, it does not say
anything about how clear() behaves regarding the capacity.




Alf P. Steinbach

unread,
Oct 13, 2017, 5:03:04 PM10/13/17
to
You can just resize it to 0.

<url: http://en.cppreference.com/w/cpp/container/vector/resize>

“Vector capacity is never reduced when resizing to smaller size because
that would invalidate all iterators, rather than only the ones that
would be invalidated by the equivalent sequence of pop_back() calls.”

In the other direction, to guarantee capacity reduction just swap with a
vector with the desired new capacity.


Cheers & hth.,

- Alf
0 new messages