Topic: std::vector capacity and reserve
Author: anthony_w.geo@yahoo.com (Anthony Williams)
Date: Tue, 26 Jul 2005 15:43:38 GMT Raw View
no.spam@no.spam.com (Maciej Sobczak) writes:
> It appears that standard requires that vector::reserve guarantees memory
> *reallocation* when the requested size is greater than the current capacity
> of the vector (the English wording is "if and only if" in 23.2.4.2/2).
>
> I think that on some platforms, where address range reservation is possible
> without actually commiting any memory, it would be possible to implement
> vector::reserve without requiring memory (re)allocation - the consecutive
> portions of "real" memory would be then commited when necessary, which is at
> the push_back or insert time.
>
> I'm aware of the fact that it would then mean that push_back or insert could
> result in bad_alloc even when the current size is less than capacity of the
> vector.
The implementation can use any technique it likes "under the covers", provided
that the user level guarantees are maintained. In the case of push_back and
insert, the guarantee is that there is no reallocation unless the new size is
greater than the old capacity. Reallocation (including that done by reserve)
is always done by allocator::allocate, which may throw std::bad_alloc.
Careful reading of the standard does not preclude vector::insert (and by
inference push_back) throwing std::bad_alloc under other circumstances, so I
therefore conclude that an implementation is allowed to return uncommitted
memory from std::allocator::allocate, and throw std::bad_alloc if it fails to
be committed in a call to vector::insert.
I think this violates the intent, because part of the reason for calling
reserve is to avoid exceptions due to allocation failures (the other one is to
avoid unnecessary copying during reallocations). Is this therefore a defect?
Anthony
--
Anthony Williams
Software Developer
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: no.spam@no.spam.com (Maciej Sobczak)
Date: Mon, 25 Jul 2005 19:05:39 GMT Raw View
It appears that standard requires that vector::reserve guarantees memory
*reallocation* when the requested size is greater than the current
capacity of the vector (the English wording is "if and only if" in
23.2.4.2/2).
I think that on some platforms, where address range reservation is
possible without actually commiting any memory, it would be possible to
implement vector::reserve without requiring memory (re)allocation - the
consecutive portions of "real" memory would be then commited when
necessary, which is at the push_back or insert time.
I'm aware of the fact that it would then mean that push_back or insert
could result in bad_alloc even when the current size is less than
capacity of the vector.
What was the real intent of this wording?
Note that some library documentations avoid mentioning "reallocation"
when describing vector::reserve, meaning that the technique mentioned
above (or any other that allows reserve *not* to reallocate even when n
> capacity) was probably considered as open - would it be legal?
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]