6 ms·
And if you dare to combine two of the paradigms it supports, you get UB.
by codeflo 2y ago
And if you dare to combine two of the paradigms it supports, you get UB.
- snappythrowaway 2y agoWhat is UB?
- MaxBarraclough 2y agoUndefined behaviour. Here's an article on the topic: https://cryptoservices.github.io/fde/2018/11/30/undefined-behavior.html https://cryptoservices.github.io/fde/2018/11/30/undefined-be...
- deleted 2y ago[deleted]
- shadowgovt 2y agoIt's what happens when you make a union of an int and a float and write it as an int and read it as a float. Most compilers will do something like "treat the bits like they represent a float, even though they mean something else when they're treated as an int." But the language spec says the compiler is allowed to send an email to Bjarne Stroustrup with your home address so he can come over and personally set your computer on fire.
- GuB-42 2y agoI bet on Bjarne Stroustrup being too busy setting other programmers computers on fire before coming to mine. More seriously, the typical response to undefined behavior is for the compiler to optimize out whatever code may trigger it. It is great for performance and one of the reasons C and C++ often top the charts, but it may lead to weird bugs that depend on the optimization level. For the union case, the compiler could completely remove the code that reads the float: because it is UB, it is something you shouldn't do, so it considers that you will never do it, so it can just remove the code. In the end your conversion function may do nothing at all (very optimized!). In practice, because it is so common, even though it is UB, it will usually do what you want it to do.