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

Reply via email to