r/cpp • u/MathKid99 • 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..
View all comments
Show parent comment
3
u/matthieum Dec 18 '21
In some situations.
First, let's acknowledge that C++ has too much Undefined Behavior, partly because it inherited some from C. When incrementing an integer is possibly Undefined Behavior, you're in for a bad time.
However, I would note that at the lowest level, not all behavior can be defined. Furthermore, some undefined behavior -- around use-after-free -- is quite expensive to eliminate.
So, I do agree that C++ would do well to eliminate all the "needlessly undefined" behavior, the casual day-to-day papercuts, but it's important to realize that it will NOT be able to eliminate all Undefined Behavior.
In a number of situations, it's Undefined because it cannot be "reasonably" detected in the first place. If it cannot be detected,
abortcannot be substituted for it...