Actual lifetime of static array slices?

Siarhei Siamashka siarhei.siamashka at gmail.com
Tue Nov 15 02:50:44 UTC 2022


On Tuesday, 15 November 2022 at 02:26:41 UTC, Elfstone wrote:
> By assigning aSlice to arr or a, it seemingly escapes the 
> scope, I thought there'd be errors, but the code compiles just 
> fine.
>
> Is it really safe though?

No, it's not safe. You can add `@safe:` line in the beginning of 
your program and it will fail to compile (after renaming 
static_array to aSlice):

     test.d(27): Error: address of variable `aSlice` assigned to 
`arr` with longer lifetime

By default everything is assumed to be @system and the compiler 
silently allows you to shoot yourself in the foot. See 
https://dlang.org/spec/memory-safe-d.html


More information about the Digitalmars-d-learn mailing list