VLA in Assembler

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 17 02:59:08 PST 2014


Foo:

> Hi,
> Could someone explain me, if and how it is possible to allocate 
> a variable length array with inline assembly?
> Somewhat like
> ----
> int[] arr;
> int n = 42;
> asm {
>     // allocate n stack space for arr
> }
> ----
> I know it is dangerous and all that, but I just want it know. ;)

Doing it with alloca is simpler:


void main() @nogc {
     import core.stdc.stdlib: alloca, exit;

     alias T = int;
     enum n = 42;

     auto ptr = cast(T*)alloca(T.sizeof * n);
     if (ptr == null)
         exit(1); // Or throw a memory error.
     auto arr = ptr[0 .. n];
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list