June 14, 2018
On 6/14/18 7:07 AM, Guillaume Piolat wrote:
> On Tuesday, 12 June 2018 at 15:35:42 UTC, Steven Schveighoffer wrote:
>>
>> No, that's not what I mean. What I mean is:
>>
>> int[] arr = [1,2,3].s;
>> int[] arr2 = [4,5,6].s;
>>
>> Legally, the compiler is allowed to reuse the stack memory allocated for arr for arr2. The lifetime of the arr data is over.
>>
>> -Steve
> 
> https://github.com/p0nce/d-idioms/issues/150
> 
> Especially if the stdlib has a way to do this now.

The Phobos PR isn't merged yet, so I think it's still valid to have this idiom, but it needs to be changed such that the implicit slicing doesn't happen.

To be clear, I think the static array idiom is useful for many reasons (my favorite is avoiding issues with literal and type size mismatch), it just that the showcase usage leads to an instant dangling pointer and should be altered.

-Steve
June 14, 2018
On Thursday, June 14, 2018 08:40:10 Steven Schveighoffer via Digitalmars-d- learn wrote:
> On 6/14/18 7:07 AM, Guillaume Piolat wrote:
> > On Tuesday, 12 June 2018 at 15:35:42 UTC, Steven Schveighoffer wrote:
> >> No, that's not what I mean. What I mean is:
> >>
> >> int[] arr = [1,2,3].s;
> >> int[] arr2 = [4,5,6].s;
> >>
> >> Legally, the compiler is allowed to reuse the stack memory allocated for arr for arr2. The lifetime of the arr data is over.
> >>
> >> -Steve
> >
> > https://github.com/p0nce/d-idioms/issues/150
> >
> > Especially if the stdlib has a way to do this now.
>
> The Phobos PR isn't merged yet, so I think it's still valid to have this idiom, but it needs to be changed such that the implicit slicing doesn't happen.
>
> To be clear, I think the static array idiom is useful for many reasons (my favorite is avoiding issues with literal and type size mismatch), it just that the showcase usage leads to an instant dangling pointer and should be altered.

Yeah. The problem is not the function to create a static array. The problem is that the return value is being sliced rather than storing it on the stack and then slicing the local variable. Slicing the return value was never a safe thing to do, and if the compiler is now giving an error when you try to do it, that's actually a really good thing.

- Jonathan M Davis

1 2
Next ›   Last »