dynamically allocating on the stack

Steven Schveighoffer schveiguy at yahoo.com
Sun Apr 22 03:11:48 UTC 2018


On 4/21/18 7:47 PM, 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.

As Nick says, it's an intrinsic. The call never gets to the C library. 
And it can't anyway -- if you give it some thought, you will realize, 
there's no way for a function to add stack space in the calling 
function. The compiler has to deal with that new space somehow, and make 
sure it puts it in the right place.

So you don't need a C library. I frankly don't know why it's in 
core.stdc, except maybe to be consistent with C?

-Steve


More information about the Digitalmars-d-learn mailing list