inferred size for static array initialization

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 2 12:19:28 PDT 2016


On 5/2/16 3:12 PM, Namespace wrote:
> On Monday, 2 May 2016 at 19:08:52 UTC, Steven Schveighoffer wrote:
>> On 5/2/16 3:02 PM, Namespace wrote:
>>> On Monday, 2 May 2016 at 18:57:49 UTC, Namespace wrote:
>>>>> A slice of a no-longer-existing temporary! Admittedly, this is not an
>>>>> issue with your code, but a deeper issue of allowing slicing of
>>>>> rvalues.
>>>> This works:
>>>> ----
>>>> int[] as = [1, 2, 3].s;
>>>> writeln(as[2]);
>>>
>>> Of course this slice is only valid as long as you dont leave the scope.
>>> That's maybe what you tried to say, right?
>>
>> No, because 'as' is pointing at stack space no longer allocated. It
>> may work, and it may not, but having it work is not proof that it's
>> sound.
>>
>> To make things clear, this is what your code effectively does:
>>
>> int[] as = void;
>> {
>>    auto _tmp = [1, 2, 3].s;
>>    as = _tmp;
>> }
>>
>> Which is not the same thing as having the static array a defined stack
>> variable in the same scope.
>>
>
> The assembler looks different than that but I may be wrong and I only
> looked at gdc.
> But anyway, that means with 'pragma(inline, true)' I'm save. Is that right?

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?

But in any case, I don't know the answer to the pragma(inline) thing. I 
would guess it is subject to the same issues, but I'm not 100% sure.

-Steve


More information about the Digitalmars-d-learn mailing list