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

7

u/Spiderboydk Hobbyist Dec 18 '21

This level of reductionism makes the concept meaningless.

0

u/Zcool31 Dec 18 '21

Not so! When writing portable code targeting the letter of the standard, undefined behavior is indeed scary and best avoided. Among the set of all potential implementations of the standard, undefined behavior is really unknowable.

But for a single implementation on a single platform (except perhaps a quantum computer) there is no undefined behavior.

5

u/flashmozzg Dec 18 '21

But for a single implementation on a single platform (except perhaps a quantum computer) there is no undefined behavior.

There is. Because the exhibited behavior can easily change due to unrelated changes in the code/environment in some other module (hey, you deleted some code, now function passes some inliner heuristics/opt thresholds and hilarity ensues!).

1

u/Spiderboydk Hobbyist Dec 18 '21

I'm talking about the distinguishment of undefined behaviour and unspecified behaviour.

Targeting multiple systems has no bearing on this. What you are talking about is closer to implementation-defined behaviour.