alloca is slow and dangerous

Max Haughton maxhaton at gmail.com
Mon Jan 4 19:49:43 UTC 2021


On Monday, 4 January 2021 at 19:38:48 UTC, Johan wrote:
> On Sunday, 3 January 2021 at 19:15:05 UTC, welkam wrote:
>> [...]
>
> Why wouldn't it?
>
> In C/C++/D/... language land, there is no such thing as "_the_ 
> stack". Yeah, the _compiler_ may decide to use the special 
> instructions/register of the CPU that address "the stack", but 
> there is no guarantee it will; nor is there a guarantee that 
> the binary executable with CPU stack instructions will actually 
> use the stack and not just dynamically allocate things. There 
> are CPUs that do not have such special instructions, and there 
> are platforms that do not use a "stack" in the common 
> interpretation of the word to execute a D program.
> Examples: Webassembly, an executable running with ASan's 
> FakeStack enabled, a binary running in an emulator, ...
>
> LDC emits the same LLVM IR "alloca" instruction for local 
> variables (`int i;`) as for `alloca` function calls. 
> Simplified: if you can write `int i;` for your platform, 
> `core.stdc.stdlib.alloca` also works. ;)
>
> -Johan

Nothing D will ever be used on will have this issue but I think 
some niche architectures don't have alloca a la C. I've seen some 
VLIW that can't use it as easily but I can't remember which one's 
exactly


More information about the Digitalmars-d mailing list