Alocating memory depending of a variable value INT variable

bearophile bearophileHUGS at lycos.com
Tue Nov 19 16:02:41 PST 2013


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.


If the OP really wants to allocate on the stack, there is the 
alloca() function (untested, I don't remember the correct name 
for the memory error):


auto ptr = cast(int*)alloca(num * 2 * int.sizeof);
if (ptr == null)
     throw new outOfMemoryError("...");
auto array = ptr[0 .. num * 2];


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list