Memory safe in D
Walter Bright
newshound2 at digitalmars.com
Tue Mar 12 17:53:41 UTC 2024
On 3/12/2024 9:13 AM, Lance Bachmeier wrote:
> You can write `ExtraInfo extra = null;`.
>
> The reason `ExtraInfo extra;` is so confusing, and leads to posts like the one
> that started this thread, is because you're explicitly telling the compiler you
> want ExtraInfo. A new user of the language has no reason to expect it to be
> null. Someone wanting to optimize their code should have to be explicit that
> they want null and they're willing to deal with all the problems that can cause.
Should it be initialized to - what? Let's say you're creating a linked list,
with null signifying the end. If there aren't null references, you're going to
have to have an "end" marker or some sort. So instead of checking for null, you
have to check for the marker. If you forget to check for the marker, and the
linked list goes off the end, then what? An exception is thrown? An assert
fail()? How are these better? The program still fails at runtime.
> While it's true that your program is always going to crash, that's not a great
> solution unless you're testing every possible outcome for your program as you
> write it. It can take a long time for it to crash, possibly when you're busy
> with other things, and with no indication of why it crashed.
In my experience, the beauty of a null pointer exception is it almost always
results in a direct indication to where the problem is, and it's one of the
easiest bugs to fix.
More information about the Digitalmars-d
mailing list