Tutorial by Examples

int *ptr = nullptr; *ptr = 1; // Undefined behavior This is undefined behavior, because a null pointer does not point to any valid object, so there is no object at *ptr to write to. Although this most often causes a segmentation fault, it is undefined and anything can happen.
Omitting the return statement in a function which is has a return type that is not void is undefined behavior. int function() { // Missing return statement } int main() { function(); //Undefined Behavior } Most modern day compilers emit a warning at compile time for this kind o...
C++11 char *str = "hello world"; str[0] = 'H'; "hello world" is a string literal, so modifying it gives undefined behaviour. The initialisation of str in the above example was formally deprecated (scheduled for removal from a future version of the standard) in C++03. A num...
It is undefined behavior to access an index that is out of bounds for an array (or standard library container for that matter, as they are all implemented using a raw array): int array[] = {1, 2, 3, 4, 5}; array[5] = 0; // Undefined behavior It is allowed to have a pointer pointing to the en...
int x = 5 / 0; // Undefined behavior Division by 0 is mathematically undefined, and as such it makes sense that this is undefined behavior. However: float x = 5.0f / 0.0f; // x is +infinity Most implementation implement IEEE-754, which defines floating point division by zero to return N...
int x = INT_MAX + 1; // x can be anything -> Undefined behavior If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. (C++11 Standard paragraph 5/4) This is one of the mo...
int a; std::cout << a; // Undefined behavior! This results in undefined behavior, because a is uninitialised. It is often, incorrectly, claimed that this is because the value is "indeterminate", or "whatever value was in that memory location before". However, it is th...
If a class, enum, inline function, template, or member of a template has external linkage and is defined in multiple translation units, all definitions must be identical or the behavior is undefined according to the One Definition Rule (ODR). foo.h: class Foo { public: double x; private...
An object can only be deallocated by delete if it was allocated by new and is not an array. If the argument to delete was not returned by new or is an array, the behavior is undefined. An object can only be deallocated by delete[] if it was allocated by new and is an array. If the argument to delet...
In most cases, it is illegal to access an object of one type as though it were a different type (disregarding cv-qualifiers). Example: float x = 42; int y = reinterpret_cast<int&>(x); The result is undefined behavior. There are some exceptions to this strict aliasing rule: An obj...
If an arithmetic operation that yields a floating point type produces a value that is not in the range of representable values of the result type, the behavior is undefined according to the C++ standard, but may be defined by other standards the machine might conform to, such as IEEE 754. float x =...
The Standard (10.4) states: Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is u...
class base { }; class derived: public base { }; int main() { base* p = new derived(); delete p; // The is undefined behavior! } In section [expr.delete] §5.3.5/3 the standard says that if delete is called on an object whose static type does not have a virtual destructor: if the...
It is illegal to access a reference to an object that has gone out of scope or been otherwise destroyed. Such a reference is said to be dangling since it no longer refers to a valid object. #include <iostream> int& getX() { int x = 42; return x; } int main() { int& r...
The standard (17.6.4.2.1/1) generally forbids extending the std namespace: The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. The same goes for posix (17.6.4.2.2/1): The behavi...
If, during the conversion of: an integer type to a floating point type, a floating point type to an integer type, or a floating point type to a shorter floating point type, the source value is outside the range of values that can be represented in the destination type, the result is undefine...
If static_cast is used to convert a pointer (resp. reference) to base class to a pointer (resp. reference) to derived class, but the operand does not point (resp. refer) to an object of the derived class type, the behavior is undefined. See Base to derived conversion.
In order to call a function through a function pointer, the function pointer's type must exactly match the function's type. Otherwise, the behaviour is undefined. Example: int f(); void (*p)() = reinterpret_cast<void(*)()>(f); p(); // undefined
Any attempt to modify a const object results in undefined behavior. This applies to const variables, members of const objects, and class members declared const. (However, a mutable member of a const object is not const.) Such an attempt can be made through const_cast: const int x = 123; const_cas...
When accessing a non-static member of an object through a pointer to member, if the object does not actually contain the member denoted by the pointer, the behavior is undefined. (Such a pointer to member can be obtained through static_cast.) struct Base { int x; }; struct Derived : Base { int y; ...

Page 1 of 2