alloca is slow and dangerous
Johan
j at j.nl
Mon Jan 4 19:38:48 UTC 2021
On Sunday, 3 January 2021 at 19:15:05 UTC, welkam wrote:
> On Friday, 1 January 2021 at 17:55:33 UTC, Steven Schveighoffer
> wrote:
>> D has alloca. It's in core.std.stdlib
>>
>> https://dlang.org/phobos/core_stdc_stdlib.html#.alloca
>>
> Does it work on all compilers and all platforms?
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
More information about the Digitalmars-d
mailing list