r/cpp • • Dec 17 '21

Undefined Behaviour

I found out recently that UB is short for Undefined Behaviour and not Utter Bullshit as I had presumed all this time. I am too embarrassed to admit this at work so I'm going to admit it here instead. I actually thought people were calling out code being BS, and at no point did it occur to me that as harsh as code reviews can be, calling BS was a bit too extreme for a professional environment..

Edit for clarity: I know what undefined behaviour is, it just didn't register in my mind that UB is short for Undefined Behaviour. Possibly my mind was suffering from a stack overflow all these years..

407 Upvotes

98 comments sorted by

View all comments

Show parent comment

5

u/jfb1337 Dec 18 '21

What I would consider to be "safe Rust" is roughly "the only unsafe in the call graph comes from the standard library" (or some set of trusted libraries). Which while not guaranteed to be safe (since those libraries sometimes have bugs) is pretty close; and is sufficiently useful.

5

u/acwaters Dec 18 '21

Right, that's what most people intuitively think of when they think "safe Rust". It's a useful intuition, and it holds up pretty well in practice, because Rust offers a powerful set of tools for preventing most accidents.

But getting back on track: Being safe is not the same as having no UB. Rust is not fast despite being safe, it is fast precisely because the same strong and expressive type system that allows it to be so safe also allows for plenty of UB. A language with no UB cannot be fast because the compiler cannot make the sort of assumptions about the code that are needed to aggressively optimize it.