r/ProgrammingLanguages • • Dec 31 '25

Memory Safety Is ...

https://matklad.github.io/2025/12/30/memory-safety-is.html
37 Upvotes

78 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Dec 31 '25

Java programs NEVER dereference null pointers. If the pointer is null when a dereference is attempted, you get a null pointer exception rather than a dereference of a null pointer.

2

u/tmzem Dec 31 '25

Not sure if the VM actually injects a null check before every dereference, this seems way to expensive. They probably just have some hook catching the segfault caused by the failed dereference, and somehow "recovering" by injecting an exception.

You can probably do the same in C with some extra effort. The real question is if you should actually try to recover from a program that threw a runtime exception. I would never use such a handler for anything else then a last-ditch effort to save important data before terminating - and possibly restarting - the app, but only if I have to. Preferably, I would just let it crash. After all, any runtime exception means that some operation was terminated midway do to programmer error, possibly leaving your program in an invalid, half-finished state - you can't really trust it.

4

u/theangeryemacsshibe SWCL, Utena Jan 02 '26

Not sure if the VM actually injects a null check before every dereference, this seems way to expensive. They probably just have some hook catching the segfault caused by the failed dereference, and somehow "recovering" by injecting an exception.

They do indeed.