[RFC] Throwing an exception with null pointers
user1234
user1234 at 12.de
Sun Apr 13 14:15:31 UTC 2025
On Saturday, 12 April 2025 at 23:11:41 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> I've been looking once again at having an exception being
> thrown on null pointer dereferencing.
>
> [...]
>
> ## Read barriers
>
> To have a 100% solution for within D code there is really only
> one option: read barriers.
> We already have them for bounds checks.
> And they can throw a D exception without any problems, plus
> they bypass the cpu/kernel guarantees which can result in
> infinite loops.
>
> This catches logic problems, but not program corruption where
> pointers point to something that they shouldn't.
>
> There is one major problem with a read barrier on pointers, how
> do you disable it?
> With slices you can access the pointer directly and do the
> dereference that by-passes it.
> Sadly we'd be stuck with either a storage class or attribute to
> turn it off.
I think that you actually dont need any language addition at all.
The read barriers can be considered as implicit, "opt-in",
contracts and whether they are codegened can be controlled with a
simple command line switch.
At first glance this system may appear costly. This is exact but
there are actually many cases for which the compiler can
determine that it has not to add a barrier.
More information about the Digitalmars-d
mailing list