Boost logo

Boost :

Subject: Re: [boost] Interest check: container with preallocated buffer
From: Dmitry Vinogradov (sraider_at_[hidden])
Date: 2009-05-26 09:33:35

OvermindDL1 wrote:
>> Thorsten Ottosen wrote:
>>> Dmitry Vinogradov skrev:
>>>> Boost.Array offers constant size container with preallocated buffer,
>>>> Boost.Optional provides from zero to one element. There are no STL container
>>>> with preallocated buffer and it seems custom allocator does not help to do
>>>> this with existing containers.
>>>> Does any container exist to offer functionality like Boost.Array but
>>>> allowing to store from 0 to N elements? Is there any interest in such
>>>> container?
>>>> PS. It's similar to fixed_string but as a generic container.
>>> Please see
> I am curious. Would it not be possible to implement a class that
> handles memory allocations of the standard STL style and just pass an
> instance of that allocator (after allocated on the stack of course) to
> the stl::vector template parameter, or does std::vector require a
> class type? I have not read the standard in this area, and from above
> I guess the C++0x is more clear on this?

Writing custom allocator can help to solve problem, but preformance will
fall. Std::vector does not have GrowPolicy template parameter, so there
is no way to tell it about pre-allocated buffer size.

Every time vector think it needs to grow, it does reallocation by
copying data from previous location to a new one. It's not nessesary
when a new size() is less than size of buffer. Then if std::vector will
grow by twiceing its size from 32 (for example) to 64 to store 50
objects, it will force the allocator to use heap - but imagine that the
buffer buffer size is 60, so heap allocating is not required at all!

Boost list run by bdawes at, gregod at, cpdaniel at, john at