Returning variable-sized stack data

Nick Treleaven nick at geany.org
Wed Jul 17 09:31:37 UTC 2024


On Wednesday, 17 July 2024 at 08:23:45 UTC, IchorDev wrote:
> On Monday, 15 July 2024 at 17:20:57 UTC, monkyyy wrote:
>> ```d
>> int[$] foo(size_t i){
>>   int[i] output;

Isn't that a stack overflow footgun?

>>   foreach(...){
>>     ...
>>   }
>>   return output;
>> }
>> ```
>
> Oh yes, that would be fantastic!
> It'd be lowered to something like this, right?
> ```d
> int[] output = (cast(int*)alloca(i * int.sizeof))[0..i];
> ```

Wouldn't that allocate on `foo`'s stack? It needs to be on the 
caller's stack.



More information about the dip.ideas mailing list