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

1

u/sagittarius_ack Jan 03 '26

Dereferencing C or dividing by 0 is actually "fine" in the sense that nothing ill-behaved happens

It really depends. From the point of view of the semantics of a programming language, the operation of dividing by 0 is undefined. In a language like C division by 0 is considered `undefined behavior`, which means that (from the point of view of the language) anything can happen.

Perhaps you could say that in practice "nothing ill-behaved happens" at the OS or hardware level. But a programing language doesn't typically say anything about what should happen at the OS or hardware level when, for example, a null pointer is dereferenced in a program. In fact, the C standard doesn't even say that the null pointer is the same as the address 0. This is from Wikipedia:

In C, dereferencing a null pointer is undefined behavior. Many implementations cause such code to result in the program being halted with an access violation, because the null pointer representation is chosen to be an address that is never allocated by the system for storing objects. However, this behavior is not universal. It is also not guaranteed, since compilers are permitted to optimize programs under the assumption that they are free of undefined behavior.

1

u/koflerdavid Jan 03 '26

Both points are true, however, segfaulting and hardware exceptions are the actual behaviors on common platforms. "Undefined" does not mean "arbitrary", although aborting with a helpful error message is for sure better. And I'm also aware that the segmentation fault will not occur if the null pointer is used with a sufficiently large index.

As for optimizations that assume there are no null pointers or division by zero problems: they are simply incorrect since one cannot assume these properties unless they have been verified in some way.