About std::vector::resize().

Carlo Wood carlo@alinoe.com
Wed May 26 10:52:00 GMT 2004


On Tue, May 25, 2004 at 01:05:04PM -0600, Martin Sebor wrote:
> Theodore Papadopoulo wrote:
> The code below must not abort for any (n > 0) but if resize()
> constructed a new vector and swapped *this for it, it would.
> 
>     std::vector<T> v;
>     v.resize (1);
>     v.reserve (n);
>     T *p = &v [0];
>     v.resize (n);   // must not reallocate

Because the (reserved) "size" is already n there is no need
for reallocation - so the mentioned algorithm will not
be used.

We are only interested to discuss the case in which
reallocation is needed (and THUS where the 'reserved'
size is too small) otherwise the algorithm is not
relevant.

-- 
Carlo Wood <carlo@alinoe.com>



More information about the Libstdc++ mailing list