More on C++ stack arrays

Tove tove at fransson.se
Sun Oct 20 13:39:12 PDT 2013


On Sunday, 20 October 2013 at 19:42:29 UTC, Walter Bright wrote:
> On 10/20/2013 12:23 PM, bearophile wrote:
>> Walter Bright:
>>
>
> No. But I do know that alloca() causes pessimizations in the 
> code generation, and it costs many instructions to execute. 
> Allocating fixed size things on the stack executes zero 
> instructions.

1) Alloca allows allocating in the parent context, which is 
guaranteed to elide copying, without relying on a "sufficiently 
smart compiler".

ref E stalloc(E)(ref E mem = *(cast(E*)alloca(E.sizeof)))
{
   return mem;
}

2) If only accessing the previous function parameter was 
supported(which is just an arbitrary restriction), it would be 
sufficient to create a helper-function to implement VLA:s.

3) Your "fixed size stack allocation" could be combined with 
alloca also, in which case it likely would be faster still.


More information about the Digitalmars-d mailing list