cpp/language/ub

Renders the entire program meaningless if certain rules of the language are violated.

Explanation
The C++ standard precisely defines the of every C++ program that does not fall into one of the following classes:
 * ill-formed - the program has syntax errors or diagnosable semantic errors. A conforming C++ compiler is required to issue a diagnostic, even if it defines a language extension that assigns meaning to such code (such as with variable-length arrays). The text of the standard uses shall, shall not, and ill-formed to indicate these requirements.
 * ill-formed,  - the program has semantic errors which may not be diagnosable in general case (e.g. violations of the or other errors that are only detectable at link time). The behavior is undefined if such program is executed.
 * implementation-defined behavior - the behavior of the program varies between implementations, and the conforming implementation must document the effects of each behavior. For example, the type of std or the number of bits in a byte, or the text of std. A subset of implementation-defined behavior is locale-specific behavior, which depends on the implementation-supplied locale.
 * unspecified behavior - the behavior of the program varies between implementations, and the conforming implementation is not required to document the effects of each behavior. For example,, whether identical s are distinct, the amount of array allocation overhead, etc. Each unspecified behavior results in one of a set of valid results.
 * undefined behavior - there are no restrictions on the behavior of the program. Examples of undefined behavior are data races, memory accesses outside of array bounds, signed integer overflow, null pointer dereference, modifications of the same scalar in an expression, access to an object through , etc. Compilers are not required to diagnose undefined behavior (although many simple situations are diagnosed), and the compiled program is not required to do anything meaningful.

UB and optimization
Because correct C++ programs are free of undefined behavior, compilers may produce unexpected results when a program that actually has UB is compiled with optimization enabled:

For example,

Signed overflow
may be compiled as (demo)

Access out of bounds
May be compiled as (demo)

Uninitialized scalar
May be compiled as (demo)

Invalid scalar
May be compiled as (demo)

Null pointer dereference
may be compiled as (foo with gcc, bar with clang)