Memory safe in D
Steven Schveighoffer
schveiguy at gmail.com
Mon Mar 11 19:43:33 UTC 2024
On Monday, 11 March 2024 at 08:16:13 UTC, Alex wrote:
> Is it expected behavior?
> Looks like it is not very safe approach and can lead to very
> unpleasant memory errors...
So I know there are a lot of responses here, with a lot of
discussion. But I don't think anyone has told you *why* D works
this way.
The explanation is that D is expecting the memory hardware to
fault when you dereference null. We know that this is not the
case for all situations, but it is the case for all of D's normal
usage modes (e.g. as user-code on standard operating systems).
Since the memory hardware *already supports this*, and is
essentially free, D has deferred to that mechanism to guard
against dereferencing null pointers. Not assuming this behavior
means all dereferences of pointers/classes in `@safe` code would
have to be instrumented with a check, slowing down the code
significantly.
I consider null pointer faults to be annoying, but not nearly as
bad as dangling pointer accesses. At least a null pointer
*always* crashes when you access it.
-Steve
More information about the Digitalmars-d
mailing list