Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems the C++ standard (2003) implicitly guarantees that the memory is not reallocated if you call the <code>clear()</code> or <code>erase()</code> method of the <code>std::vector</code>.</p> <p>According to the requirements of the Sequence (table 67) the <code>a.clear()</code> is equivalent to <code>a.erase(begin(),end())</code>.</p> <p>Furthermore, the standard states that the <code>erase(...)</code> member function of the <code>std::vector&lt;T&gt;</code> does not throw an exception unless one is thrown by the copy constructor of T (section 23.2.4.3). Hence it is implicitly guaranteed, because a reallocation could cause an exception (sections 3.7.3, 20.4.1.1).</p> <p>Also <code>v.begin()</code> remains the same, as <code>erase(...)</code> will only invalidate all iterators after the point of the erase (section 23.2.4.3). However, it won't be dereferenceable (since <code>v.begin() == v.end()</code>).</p> <p>So, if you have a standard compliant implementation you are fine...</p> <p><strong>CORRECTION</strong></p> <p>My reasoning is flawed. I managed to show that <code>erase(...)</code> does not reallocate, but an implementation could still deallocate the memory if you erase all elements. However, if <code>a.capacity()</code> reports "you can add N elements without reallocating memory" after the erase/clear you are fine.</p> <p>The C++11 standard defines <code>a.clear()</code> without referring to <code>a.erase(...)</code>. <code>a.clear()</code> is not allowed to throw an exception. Hence it could deallocate, but not reallocate. So you should check the capacity after clearing the vector to make sure that the memory is still there and the next resize won't reallocate.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload