<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1102: std::vector's reallocation policy still unclear</title>
<meta property="og:title" content="Issue 1102: std::vector's reallocation policy still unclear">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1102.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#Open">Open</a> status.</em></p>
<h3 id="1102"><a href="lwg-active.html#1102">1102</a>. <code>std::vector</code>'s reallocation policy still unclear</h3>
<p><b>Section:</b> 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2009-04-20 <b>Last modified:</b> 2020-07-17</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#vector.capacity">active issues</a> in [vector.capacity].</p>
<p><b>View all other</b> <a href="lwg-index.html#vector.capacity">issues</a> in [vector.capacity].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>
<p>
I have the impression that even the wording of current draft
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2857.pdf">N2857</a>
does insufficiently express the intent of <code>vector</code>'s
reallocation strategy. This has produced not too old library
implementations which release memory in the <code>clear()</code> function
and even modern articles about C++ programming cultivate
the belief that <code>clear</code> is allowed to do exactly this. A typical
example is something like this:
</p>

<blockquote><pre>
const int buf_size = ...;
std::vector&lt;T&gt; buf(buf_size);
for (int i = 0; i &lt; some_condition; ++i) {
  buf.resize(buf_size);
  write_or_read_data(buf.data());
  buf.clear(); // Ensure that the next round get's 'zeroed' elements
}
</pre></blockquote>
<p>
where still the myth is ubiquitous that <code>buf</code> might be
allowed to reallocate it's memory <b>inside</b> the <code>for</code> loop.
</p>
<p>
IMO the problem is due to the fact, that
</p>

<ol style="list-style-type:lower-alpha">
<li>
the actual memory-reallocation stability of <code>std::vector</code>
is explained in 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a>/3 and /6 which
are describing just the effects of the <code>reserve</code>
function, but in many examples (like above) there
is no explicit call to <code>reserve</code> involved. Further-more
23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a>/6 does only mention <em>insertions</em>
and never mentions the consequences of erasing elements.
</li>
<li>
<p>
the effects clause of <code>std::vector</code>'s <code>erase</code> overloads in
23.3.13.5 <a href="https://wg21.link/vector.modifiers">[vector.modifiers]</a>/4 is silent about capacity changes. This
easily causes a misunderstanding, because the counter
parting insert functions described in 23.3.13.5 <a href="https://wg21.link/vector.modifiers">[vector.modifiers]</a>/2
explicitly say, that
</p>
<blockquote><p>
Causes reallocation if the new size is greater than the
old capacity. If no reallocation happens, all the iterators
and references before the insertion point remain valid.
</p></blockquote>
<p>
It requires a complex argumentation chain about four
different places in the standard to provide the &mdash; possibly
weak &mdash; proof that calling <code>clear()</code> also does <em>never</em> change
the capacity of the <code>std::vector</code> container. Since <code>std::vector</code>
is the de-facto replacement of C99's dynamic arrays this
type is near to a built-in type and it's specification should
be clear enough that usual programmers can trust their
own reading.
</p>
</li>
</ol>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote>
<p>
Bill believes paragraph 1 of the proposed resolution is unnecessary
because it is already implied (even if tortuously) by the current wording.
</p>
<p>
Move to Review.
</p>
</blockquote>

<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Mark as NAD. Rationale: there is no consensus to clarify the standard,
general consensus that the standard is correct as written.
</p></blockquote>

<p><i>[2020-05-08; Reopen after reflector discussions]</i></p>

<p>
"correct as written" has been disputed.
</p>

<p><i>[2020-07-17; Priority set to 3 in telecon]</i></p>



<p id="res-1102"><b>Proposed resolution:</b></p>
<p><i>[
This is a minimum version. I also
suggest that the wording explaining the allocation strategy
of <code>std::vector</code> in 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a>/3 and /6 is moved into
a separate sub paragraph of 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a> <em>before</em>
any of the prototype's are discussed, but I cannot provide
reasonable wording changes now.
]</i></p>


<ol>
<li>
<p>
Change 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a>/6 as follows:
</p>
<blockquote><p>
It is guaranteed that no reallocation takes place during
insertions <ins>or erasures</ins> that happen after a call
to <code>reserve()</code> until the time when an insertion would make
the size of the vector greater than the value of <code>capacity()</code>.
</p></blockquote>
</li>
<li>
<p>
Change 23.3.13.5 <a href="https://wg21.link/vector.modifiers">[vector.modifiers]</a>/4 as follows:
</p>
<blockquote><p>
<i>Effects:</i> <ins>The capacity shall remain unchanged and no reallocation shall
happen.</ins>
Invalidates iterators and references at or after the point
of the erase.
</p></blockquote>
</li>
</ol>





</body>
</html>
