Boost logo

Boost :

From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2001-08-18 19:48:24


 John Max Skaller wrote:
> But such an animal cannot exist. There is no way
> to allocate n elements of an array on the stack with n being
> dynamically determined -- without using VLA's. That is the whole
> point of VLA's.

You can get pretty close, though:

fast_buffer<255, char> buf(n);
char* p = buf.data();
//..

Here, if n <= 255, then no dynamic allocation will be performed, and the
buf.data() will point to a stack space; otherwise, the memory will be
allocated on the heap (and released in the destructor of the class). Of
course, in the latter case 255 bytes of the stack space will be "wasted",
but usually you don't care :). In return, if you know that a typical value
of n <= 255, but that also there are (relatively rare) cases then the
condition does not hold (one-line character buffer is a typical example of
such situation), then the 'fast_buffer' class can save you quite a few
processor cycles :). Not exactly a VLA, but in many cases close enough.

Aleksey


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