Topic: Defect Report: vector capacity, reserve and reallocation


Author: "Markus Schaaf" <m.schaaf.exp-aug2001@gmx.de>
Date: Tue, 7 Aug 2001 11:37:04 GMT
Raw View
"Anthony Williams" <anthwil@nortelnetworks.com> wrote:

> Change the wording of 23.2.4.5p5 to:
>
> "Notes: Reallocation invalidates all the references, pointers, and iterators
> referring to the elements in the sequence. It is guaranteed that no
> reallocation takes 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() after the most recent call to
> reserve()."

I'm astonished by the mention of "reserve" at all. If vectors
have that property called "capacity", you should be able to
rely on it. The wording above (and that currently in the
standard, too) suggests that reallocation may occur during
the call to "push_back" in the following code:

    std::vector< int > v;
    // ... no use of v.reserve() ...
    if( v.capacity() > v.size() )  v.push_back( 3 );

If that is not what was intended, just state it clear and
simple:

"Notes: Reallocation invalidates all the references, pointers,
and iterators referring to the elements in the sequence. It is
guaranteed that no reallocation takes place during insertions
until the time when an insertion would make the size of the
vector greater than the value of capacity()."

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: 14 Jul 01 06:02:16 GMT
Raw View
 [Moderator's note: this defect report has been
 forwarded to the C++ committee. -moderator.]

There is an apparent contradiction about which circumstances can cause a
reallocation of a vector in Section 23.2.4.2 [lib.vector.capacity] and
section 23.2.4.3 [lib.vector.modifiers]

23.2.4.2p5 says

"Notes: Reallocation invalidates all the references, pointers, and iterators
referring to the elements in the sequence. It is guaranteed that no
reallocation takes 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 size specified in the most recent call to reserve()."

Which implies if I do

std::vector<int> vec;
vec.reserve(23);
vec.reserve(0);
vec.insert(vec.end(),1);

then the implementation may reallocate the vector for the insert, as the
size specified in the previous call to reserve was zero.

However, the previous paragraphs state:

(23.2.4.2p1) "(capacity) Returns: The total number of elements the vector
can hold without requiring reallocation"
(23.2.4.2p2) "...After reserve(), capacity() is greater or equal to the
argument of reserve if reallocation happens; and equal to the previous value
of capacity() otherwise..."

This implies that vec.capacity() is still 23, and so the insert() should not
require a reallocation, as vec.size() is 0. This is backed up by 23.2.4.3p1:

"(insert) Notes: Causes reallocation if the new size is greater than the old
capacity."

Though this doesn't rule out reallocation if the new size is less than the
old capacity, I think the intent is clear.

Suggested resolution:

Change the wording of 23.2.4.5p5 to:

"Notes: Reallocation invalidates all the references, pointers, and iterators
referring to the elements in the sequence. It is guaranteed that no
reallocation takes 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() after the most recent call to
reserve()."

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]