News for hackers. Stuff that matters.
Re:Initialization in C++ is Seriously Bonkers by adrianN on Sunday January 06, 2019 @03:10AM (#18837226)
But if you remember the underlying reason you might think that using uninitialized variables gives just gives you an arbitrary value. That's not true, it's UB. The compiler can do whatever it wants if it can prove that you read an uninitialized variable.
Re:Initialization in C++ is Seriously Bonkers by zawerf on Sunday January 06, 2019 @03:25AM (#18837275)
Do compilers really try to prove UB so they can use it as an excuse to silently murder kittens? That sounds so counterproductive. If it can prove UB, it should just pop an error and stop.
Re:Initialization in C++ is Seriously Bonkers by codeflo on Sunday January 06, 2019 @04:00AM (#18837372)
What's actually happening is a bit more subtle: The compiler is allowed to assume that the programmer in fact made no mistake and there's a reason why the UB is never actually triggered at runtime. Reasoning backwards from that can open up large optimization opportunities elsewhere, like not loading a value from memory twice, or not actually checking a condition. A good article about this that was recently discussed on HN: https://news.ycombinator.com/item?id=18575383
Re:Initialization in C++ is Seriously Bonkers by palunon on Sunday January 06, 2019 @05:34AM (#18837656)
What they do is not try to prove UB, but use it as a precondition for the proof that their optimizations don't change the program behavior - ie. they can assume that anything resulting in UB won't happen.
Re:Initialization in C++ is Seriously Bonkers by saagarjha on Sunday January 06, 2019 @05:07AM (#18837559)
> Do compilers really try to prove UB so they can use it as an excuse to silently murder kittens?

Clang in particular is quite aggressive about this, and is prone to producing surprising results. GCC has started doing this too, but it is much more conservative,