dynamically allocating on the stack
Nicholas Wilson
iamthewilsonator at hotmail.com
Sun Apr 22 00:41:34 UTC 2018
On Saturday, 21 April 2018 at 23:47:41 UTC, Mike Franklin wrote:
> On Saturday, 21 April 2018 at 19:06:52 UTC, Steven
> Schveighoffer wrote:
>
>> alloca is an intrinsic, and part of the language technically
>> -- it has to be.
>
> From what I can tell `alloca` is only available in the
> platform's C standard library (actually for Linux it appears be
> part of libgcc as `__builtin_alloca`; `alloca` is just and
> alias for it).
> Of course I can use 3rd party libraries like C to do this, but
> it seems like something useful to have in the language for
> certain use case and optimizations. Also, my immediate use
> case if for bare metal microcontroller programming where I'm
> intentionally avoid C and looking for a way to do this in
> idiomatic D.
>
> Mike
As you have discovered alloca is a builtin: this is true for LDC,
GDC and DMD, it's true for clang and i suspect most C/C++
compilers.
You're not using the C library version of it, the compiler does
the stack space reservation inline for you. There is no way
around this.
Use `std.experimental.allocator.building_blocks.Region` with a
slice from `alloca`.
or if you just want the memory
scope T[] arr = (cast(T*)alloca(n*T.sizeof))[0 .. n];
More information about the Digitalmars-d-learn
mailing list