Undefined Behaviour in C++

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++);

Resources

Tools

Strict aliasing

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.

Resources:

Share

follow