D Language Foundation October 2023 Quarterly Meeting Summary

Timon Gehr timon.gehr at gmx.ch
Mon Dec 11 19:55:38 UTC 2023


On 12/6/23 17:28, Mike Parker wrote:
> 
> 
> One way to do that in D is to use `alloca`, but that's an issue because 
> the memory it allocates has to be used in the same function that calls 
> the `alloca`. So you can't, e.g., use `alloca` to alloc memory in a 
> constructor, and that prevents using it in a custom array 
> implementation. He couldn't think of a way to translate it.

There is the following trick. Not ideal since the length cannot be 
inferred, but this successfully injects alloca into the caller's scope.

```d
import core.stdc.stdlib:alloca;
import std.range:ElementType;
import core.lifetime:moveEmplace;

struct VLA(T,alias len){
     T[] storage;
     this(R)(R initializer,return void[] 
storage=alloca(len*T.sizeof)[0..len*T.sizeof]){
         this.storage=cast(T[])storage;
         foreach(ref element;this.storage){
             assert(!initializer.empty);
             auto init=initializer.front;
             moveEmplace!T(init,element);
             initializer.popFront();
         }
     }
     ref T opIndex(size_t i)return{ return storage[i]; }
     T[] opSlice()return{ return storage; }
}

auto vla(alias len,R)(R initializer,void[] 
storage=alloca(len*ElementType!R.sizeof)[0..len*ElementType!R.sizeof]){
     return VLA!(ElementType!R,len)(initializer,storage);
}

void main(){
     import std.stdio,std.string,std.conv,std.range;
     int x=readln.strip.to!int;
     writeln(vla!x(2.repeat(x))[]);
}
```



More information about the Digitalmars-d-announce mailing list