Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-09-07 12:48:52


Of course, if you want the array to be stored on the stack, you could use a
custom allocator that contains all of the storage it can allocate in some
member which is an array of chars. Since the allocator is stored within the
vector, if the vector is allocated on the stack, the array storage will be,
too. Of course, you will just have to handle the out-of-memory condition
sooner than you would otherwise, by throwing bad_alloc.

This sounds like a very hard way to go, however.
Often this sort of problem is best solved by having some amount of storage
for elements directly in the object, and dynamically allocating if the size
grows beyond that. Some experiments have shown that people generally make
strings of 6 or fewer characters, so people talk about implementing
std::string this way.

-Dave

----- Original Message -----
From: <rwgk_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, September 07, 2001 1:18 PM
Subject: [boost] crossbreed of boost::array & std::vector

> I have an application where it would be convenient to
> use std::vector for a variety of purposes. However,
> most of the vectors are tiny, and the dynamic memory
> management overhead is significant.
> boost::array is generally a better fit, but then I have
> to keep track of the actual number of elements in a custom
> way (array::size() is a static function), and push_back()
> etc. are not available.
> What I really want is a type which looks just like
> vector, but uses a built-in array (on the stack) rather
> than new/delete. I see two choices:
> 1. Replicate std::vector based on boost::array.
> This does not seem very elegant...
> 2. provide a custom STL allocator for use with std::vector.
> After staring at Josuttis chapter 15 for a while
> I came to the preliminary conclusion that this
> approach will not work because the allocator
> necessarily has to do some kind of dynamic allocation.
> Questions:
> - Are the alternative approaches (or even solutions)?
> - Would a well worked out "array_vector" or "static_vector"
> be a useful addition to boost?
>
> Thanks!
> Ralf
>
>
>
> Info: http://www.boost.org Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk