Does 'D' language supports 'C' like VLA?

via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 14 12:12:13 PDT 2015


On Tuesday, 14 April 2015 at 10:10:17 UTC, Jacob Carlborg wrote:
> On 2015-04-14 11:50, "Marc =?UTF-8?B?U2Now7x0eiI=?= 
> <schuetzm at gmx.net>" wrote:
>
>> It does work like that, but I don't see a way to pass the 
>> length to the
>> alloca() call. Unfortunately, we can't refer to other 
>> parameters. It
>> that were possible, it would indeed work:
>>
>> import core.stdc.stdlib : alloca;
>> T[] stackArray(T)(size_t len, T* p = cast(T*) alloca(len * 
>> T.sizeof)) {
>>         return p[0 .. len];
>> }
>
> Oh, I thought that would work.

Maybe that's worth an enhancement request? I agree with 
bearophile that stack local allocations should be encouraged 
more, and bare alloca() is unfortunately unsafe. With DIP25, it 
would even be possible to make a completely @safe wrapper.


More information about the Digitalmars-d mailing list