Memory safe in D - cppfront/C++

Walter Bright newshound2 at digitalmars.com
Tue Apr 16 18:25:29 UTC 2024


On 4/11/2024 9:25 AM, Nick Treleaven wrote:
> What I meant was if there is a dereference of a pointer that *may have been* 
> (according to the limited analysis) assigned the address of a local that has 
> gone out of scope, that dereference gets flagged at compile-time. Even though at 
> runtime it may never actually have that address.

Given the following:

```
@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?


More information about the Digitalmars-d mailing list