Memory safe in D - cppfront/C++

ag0aep6g anonymous at example.com
Tue Apr 16 18:46:06 UTC 2024


On 16.04.24 20:25, Walter Bright wrote:
> ```
> @safe
> void foo()
> {
>      int* p;
>      {
>      int x;
>      p = &x;
>      }
> }
> ```
> 
> The compiler gives:
> 
> test.d(8): Error: address of variable `x` assigned to `p` with longer 
> lifetime
> 
> when the -preview=dip1021 switch is used.
> 
> https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1021.md
> 
> Perhaps it's time to make dip1021 the default. Or at least turn it on 
> with dip1000?
DIP 1021 does nothing here.

It's `-preview=dip1000` that prints the error. `-preview=dip1021` 
implies `-preview=dip1000`.

And no `-preview` is needed at all for DMD to reject the code (with a 
different error).


More information about the Digitalmars-d mailing list