@safe - why does this compile?

Johan Engelen j at j.nl
Mon Jul 16 16:23:23 UTC 2018


On Friday, 13 July 2018 at 14:51:17 UTC, ketmar wrote:
>
> yeah. in simple words: safe code is *predictable*, but not 
> "segfault-less". segfaults (null dereferences) in safe code are 
> allowed, 'cause they have completely predictable behavior 
> (instant program termination).
>
> @safe doesn't free you from doing your null checks, it protects 
> you from so-called "undefined behavior" (aka "unpredictable 
> execution results"). so when we are talking about "memory 
> safety", it doesn't mean that your code cannot segfault, it 
> means that your code won't corrupt random memory due to 
> misbehaving.

This is not true when using LDC (and I'd expect the same for GDC).
With LDC, dereferencing `null` is undefined behavior regardless 
of whether you are in an @safe context or not.

- Johan



More information about the Digitalmars-d-learn mailing list