Memory safe in D

Nick Treleaven nick at geany.org
Mon Mar 18 12:19:18 UTC 2024


On Wednesday, 13 March 2024 at 19:58:24 UTC, Steven Schveighoffer 
wrote:
> Building non-null into the type indeed means as long as you 
> have that type, you don't have to check. But to get it into 
> that type, if you started with a possibly-invalid value, 
> *somewhere* you had to do a check.
...
> Having a possibly-null pointer is no different. D defines that 
> in safe code, a pointer will be valid or null. The "check" 
> occurs on use, and is performed by the hardware.

One important difference is that D makes the null check *as late 
as possible*. Often in code using non-nullable types, the check 
gets done earlier, nearer to where the problem is. E.g. when a 
function produces a pointer, but the pointer doesn't actually get 
dereferenced there but is stored and then some time later it gets 
dereferenced. Then it's not easy to find where the null pointer 
was actually produced. Having non-null pointers can save time 
debugging.


More information about the Digitalmars-d mailing list