inferred size for static array initialization

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 2 13:05:15 PDT 2016


On 5/2/16 3:38 PM, Namespace wrote:
>> The assembler might be safe in some instances, but that doesn't
>> reflect the original internal representation in the compiler. Some
>> other configuration of calls may allow the compiler to reuse that
>> memory, and then you run into problems.
>>
>> I'm wondering if you used my rewrite if it would actually output the
>> same thing?
>
> Not quite. Look for yourself:
> https://godbolt.org/g/kO8hdW
> https://godbolt.org/g/KCfYPy

Except for offsets, it looks identical. May be the compiler puts things 
in different spots for different ways of writing.

But the thing that's not disclosed here is: what happens when the 
compiler feels the need to reuse that stack space? Your example doesn't 
have anything that may compete for the space, it just returns after this 
is over.

For example, define a static array of exactly the same size to use after 
the call:

int[3] x;

Now, see if x is pigeonholed into that same place your temporary return 
existed (and therefore 'as' points at it).

I'm still fully on the side of this being unsafe.

-Steve


More information about the Digitalmars-d-learn mailing list