Alocating memory depending of a variable value INT variable

Namespace rswhite4 at googlemail.com
Wed Nov 20 02:39:38 PST 2013


On Wednesday, 20 November 2013 at 00:02:42 UTC, bearophile wrote:
> Ali Çehreli:
>
>> That is a VLA.
>
> That are currently not present in D. The most common and safe 
> alternatives in D are allocating the memory on the heap with 
> 'new', or over-allocating on the stack a fixed size and then 
> slicing.

That's why I use a Stack and a Heap struct in combination with an 
Array struct:
Heap heap;
Stack stack;
byte[] arr = Array!byte(&stack, &heap).of(Num);
or even more naturally:
byte[] arr = Array!byte(&stack, &heap)[Num];

Stack has a buffer of e.g. 4096 and tries to allocate there, if 
it fails, it returns null. If this happens, the Heap struct 
allocates on the GC or C heap.
And if the Stack / Heap struct gets destroyed, the stored memory 
is freed.

But VLA's were really desirable. :(



More information about the Digitalmars-d-learn mailing list