3 ms·
Optimizing compilers are pretty magical. It's easier than you think to code undefined behavior. When the compiler detects undefined behavior, it can do anything
by jfoutz 12y ago
Optimizing compilers are pretty magical. It's easier than you think to code undefined behavior. When the compiler detects undefined behavior, it can do anything.
Here's a pretty neat example [1]. There have been a few writeups on HN over the years, i can't find some of the more elaborate surprising behaviors.
[1] http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- to3m 12y agoI drag these two out every time somebody mentions this stuff: http://blog.metaobject.com/2014/04/cc-osmartass.html http://blog.metaobject.com/2014/04/cc-osmartass.html, http://robertoconcerto.blogspot.co.uk/2010/10/strict-aliasing.html http://robertoconcerto.blogspot.co.uk/2010/10/strict-aliasin...
- mikeash 12y agoYou don't need an optimizing compiler to cause undefined behavior, though. Something as simple as printf("%f", 3) will do. Optimizations can expose undefined behavior that otherwise would remain unnoticed... but they can also "fix" undefined behavior that shows up without optimizations. They're really orthogonal.