Static Array Idiom not working anymore.

Steven Schveighoffer schveiguy at yahoo.com
Tue Jun 12 15:35:42 UTC 2018


On 6/12/18 10:57 AM, Guillaume Piolat wrote:
> On Tuesday, 12 June 2018 at 14:44:12 UTC, Steven Schveighoffer wrote:
>>
>> Note to ponce, please update your idioms, this is NOT safe, even 
>> within the same function. Just because it does work, doesn't mean it 
>> will always work. The language makes no guarantees once the lifetime 
>> is over.
>>
> 
> I thought it was clear enough because the comment said
> 
>      // Slice that static array __which is on stack__
> 
> but now I see how it can be hard to see the unsafety.

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.

In other words, you should NEVER do this. You should ALWAYS create a 
static array on the stack with a variable name, and then slice that if 
you need to.

-Steve


More information about the Digitalmars-d-learn mailing list