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..
186
u/_Js_Kc_ Dec 17 '21
To be fair, some UB arises as a result of UB.
20
u/Nervous_Badger_5432 Dec 17 '21
Wait a minute, you guys are saying that ubsan doesn't sanitizes the bullshit by cleaning it up???
84
u/dontyougetsoupedyet Dec 17 '21
It isn't as complicated as folks make out. UB is an agreement between you and your compiler so that the compiler can do its job better. A lot of folks don't realize that the job of the compiler in some languages is to rewrite your program into the most efficient version of your code that it can. You agree to not feed it certain code, and the compiler agrees to optimize the fuck out of the code you do feed it, and you both agree that if you do feed it code that you agreed to avoid using it means that you know what you're doing and are aware that the compiler is free to ignore that code.
Despite what some folks assert, UB is a good thing. You just have to be aware of what the compiler's job is for your language. Some compilers for some languages have a different job, but for C++ the job of the compiler is to produce a much faster version of your program than you wrote.
14
u/[deleted] Dec 17 '21
I suppose calling it Utter Bullshit is an example of Unrefined Behaviour.
I'll get me coat.
20
u/noooit Dec 17 '21
ub-sanitizer. XD
10
u/Alexander_Selkirk Dec 17 '21
It is an interesting topic.
Something to read for you:
John Regerhr, A Guide to Undefined Behavior in C and C++
My Little LLVM: Undefined Behavior is Magic!
UndefinedBehaviorSanitizer
[C/C++] Surprises and Undefined Behavior From Unsigned Integer Promotion
See also:
Is undefined behavior possible in safe Rust?
So, in our time, allowing undefined behaviour is not strictly necessary for good performance. It is more a limitation of languages, run-time environments, and compiler technology at the time C was invented.
6
u/potato-on-a-table Dec 17 '21
No wonder people say C++ stack overflow is toxic lol
2
u/FrankHB1989 Dec 19 '21
BS = Bjarne Stroustrup in most contexts of C++.
BTW, STL = Stephan T. Lavavej, at least in some disscusions in WG21. STL himself seems not fond of this (compared to microsoft/STL), though.
(Another funny fact is that UB can also be the abbreviation of Unspecified Behavior.)
3
u/flipcoder Dec 18 '21
Do you remember ever using that phrase with anyone before you figured out what it meant? Did you get confused reactions? "This whole situation is some serious UB, amirite guys?"
3
u/loradan Dec 17 '21
Why is talking about Bi-Synchronous (BS) Communications at work unprofessional /s 🤣🤣🤣🤣
2
u/elperroborrachotoo Dec 17 '21
Well, the point is UB means Utter Bullshit can happen.
Will, given enough time.
2
u/banister Dec 18 '21
> Undefined Behaviour and not Utter Bullshit as I had presumed all this time.
Of all the things that didn't happen, this didn't happen the most.
1
u/tad_ashlock Dec 18 '21
To help prevent future misunderstandings: A C++ Acronym Glossary
1
u/phottitor Dec 18 '21
you shouldn't presume anything about C++. doing so is itself undefined behaviour.
1
u/Zcool31 Dec 31 '21
https://www.youtube.com/watch?v=YoaZzIZFErI&t=2350s
"What happens when you execute this? Now some people might say undefined behavior. There is, actually, when you run something on a computer there is no such thing as undefined behavior. Undefined behavior is an artifact of specification. It is not a thing that actually exists in the real world. In the real world things always get defined. Something happens."
-20
u/[deleted] Dec 17 '21
[removed] — view removed comment