Topic: Vectors and Allocators


Author: Greg Hickman <greg.hickman@lmco.com>
Date: Mon, 10 Jun 2002 21:16:04 GMT
Raw View
I'd like to supply an allocator to std::vector that obtains memory from a
static, fixed-size buffer.  I assumed that allocator::max_size() would allow
me to bound the number of elements (and the element capacity) requested by
the container to the fixed capacity of the buffer, but after experimenting
with my compiler's library, I'm not so sure.  Specifically,

vector<int, Static_allocator<int, 100> > v;  // Static_allocator::max_size()
== 100
v.push_back(1);

doesn't work as I would hope on my implementation.  This is because the
initial number of int-sized chunks requested by vector::push_back is greater
than 100.

This experience, combined with a perhaps too cursory reading of the
standard, has left me with a number of questions.  What does
allocator::max_size()'s return value mean?  Is it the total number of
objects that can be allocated, or the maximum number that can be allocated
at a given point in time?  If the former, shouldn't vector::max_size()
necessarily be equivalent to allocator::max_size()?  Are a vector's requests
for total capacity required to be <= allocator::max_size()?  If not, how can
a vector's allocator reasonably be tied to a fixed-size buffer?

Greg Hickman



---
[ 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: Pete Becker <petebecker@acm.org>
Date: Tue, 11 Jun 2002 00:35:58 GMT
Raw View
Greg Hickman wrote:
>
> What does
> allocator::max_size()'s return value mean?  Is it the total number of
> objects that can be allocated, or the maximum number that can be allocated
> at a given point in time?

Neither. It means that if you ask for more the request will fail. It
says nothing about how large a request will succeed. It's far less
useful than it might appear.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ 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                       ]