alloca is slow and dangerous

Steven Schveighoffer schveiguy at gmail.com
Fri Jan 1 17:55:33 UTC 2021


On 1/1/21 9:48 AM, welkam wrote:
> Over the years I saw several people asking why D doesn't have alloca. I 
> just want to share a video where it states that alloca is slower than 
> static array so we have one more arguments against it.
> https://youtu.be/FY9SbqTO5GQ?t=468
> 
> In summary alloca is:
> 1. Hard to implement
> 2. security problem
> 3. slower than static array on the stack

D has alloca. It's in core.std.stdlib

https://dlang.org/phobos/core_stdc_stdlib.html#.alloca

In my experience, using it isn't extremely beneficial -- since it's in 
stdc, it requires the c library, which means you have malloc/free, which 
is much safer/usable.

I'd much rather use a static array, or malloc/scope(exit) free, or a 
combination between the two that uses the stack when it can, and expands 
to malloc when needed.

Or just use the GC...

-Steve


More information about the Digitalmars-d mailing list