From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2006-10-18 06:59:37
On 10/18/06, XHChen_at_[hidden] <XHChen_at_[hidden]> wrote:
> Dear boost interesters:
> "alloca" maybe a suitable tool to construct a variable-length array.
> The attachment demonstrates the class for variable-length array,
> most of the codes in which are copied from original "array.hpp" in boost libraries.
> Hope it is useful.
I was thinking of something like that just yesterday (that is, the way
to have dynamically sized automatic arrays). The problem of your
solution is that it uses alloca, and this function is not defined by
any standard (not only it is not in C nor C++, neither POSIX and SuS
There is no portable way to dynamically allocate space on the standard
stack, but the fact is, there is really no reason that we must use the
The idea is that the alloca is fast because you just need to increase
the SP pointer to allocate memory, but you can do that with any stack.
Have a global alternate stack (better if it is thread local) and use
it to allocate 'stacked' space. In practice your custom alloca would
allocate from it instead of the 'real stack'. If you want to wrap it
under an allocator interface, push_allocate and pop_deallocate would
be good names. This solution would have the same allocation speed of
the historical alloca and probably the same cache locality. If you
relax the condition that allocation and deallocaiton must be strictly
FIFO (i.e. you can temporarily waste memroy while a free space is
blocked by an allocated chunk above it), you could even make it a
Giovanni P. Deretta
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk