Undefined Behaviour (UB) is a situation when standard does not describe how program should perform, so program behaviour depends on selected compiler implementation. There is a number of situations that can be described as "undefined".
Example of such situation (from Bjarne Stroustrup's C++ Style and Technique FAQ)
// value of a is undefined int a = i++ + i++; // it's undefined what values will be send to foo() foo(v[i],i++);
- GCC Easter Egg: C++ Undefined Defined Behavior - Feross Aboukhadijeh, 2010
- Here Be Dragons: C++ Undefined Behavior - Pete Isensee, 2008 (known also under another title: "Chills and Thrills: Undefined Behavior in C++")
- Undefined behavior can result in time travel (among other things, but time travel is the funkiest) - msdn.com
- Both true and false: a Zen moment with C - Mark Shroyer, 2012
- Contest: Craziest Compiler Output due to Undefined Behavior - John Regehr, 2012
- A Guide to Undefined Behavior in C and C++ - John Regehr, 2010
- Why do some people think x = ++y + y++ is bad? - C++ FAQ
- What Every C Programmer Should Know About Undefined Behavior - LLVM Project Blog
- A Guide to Undefined Behaviour in C and C++
- GCC Undefined Behavior Sanitizer – ubsan - Marek Polacek, 2014: run-time checker included in GCC 4.9
- Clang - Controlling Code Generation - see -fsanitize=undefined
In C/C++ at higher optimization levels compiler follows "strict aliasing" rule which says that casting pointer of one type into another can lead to undefined behaviour and is prohibited. This rule is related to optimization that cannot happen if compiler cannot be sure if two pointers do not refer to the same memory address.