This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: vector<> can probably never grow to it's maximum size!
- From: Paolo Carlini <pcarlini at suse dot de>
- To: Dhruv Matani <dhruvbird at gmx dot net>
- Cc: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Sun, 17 Oct 2004 11:07:42 +0200
- Subject: Re: vector<> can probably never grow to it's maximum size!
- References: <1097952963.6447.12.camel@localhost.localdomain>
Dhruv Matani wrote:
Hello,
I would like to discuss an issue which deals with vector's memory
management. Consider a 16 machine.
Here, max_size() would typically be say: ((2^16)-1)/sizeof(_Tp). Now,
consider that the current size is max_size()/2. If I want to add say 3
more elements, the vector to get space for totally max_size() elements,
and will fail because operator new will probably fail because of such a
large request. Can't we have the vector settle for a slightly smaller
return from the allocator at the expense of not sticking to the standard
strictly about the exponential increase policy?
If I understand correctly your question, you are right and indeed we are
already
implementing this logic for std::string: see _M_mutate in
basic_string.tcc: we
always check not to exceed max_size and in that case we simply stay with it.
BUT BUT BUT
Be warned that in the std::vector case things are slightly more tricky than
computing min(max_size, new_size), since max_size == size_type(-1) /
sizeof(value_type) and when sizeof(value_type) == 1, max_size is already
at maximum rapresentable unsigned value, therefore, you cannot really
exceed it with new_size...
Also, I want you to audit *all* the varios places in std::vector where
we grow,
since someone pointed out time ago that there are inconsistencies. Also,
I want
to see nice testcases, with custom allocators.
Paolo.