VLA in Assembler
Foo via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Dec 17 03:39:42 PST 2014
On Wednesday, 17 December 2014 at 10:59:09 UTC, bearophile wrote:
> 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
Yes I know, but I really want it in inline assembly. It's for
learning purpose. :)
More information about the Digitalmars-d-learn
mailing list