Integer Underflow and Overflow

9 April 2021 by Phillip Johnston • Last updated 14 December 2021 This entry was contributed Nathan Jones. Thanks Nathan! Integer underflows and overflows are a common computational error that occurs in computer systems. These error conditions are often not explicitly checked for by developers. Ignoring the possibility of overflows or underflows can have disastrous results. Basic Overview Integer overflow occurs when the result of a math operation is too large to fit into the result variable’s data type. For example, the code below overflows if a = INT_MAX and b = 1. int add( int a, int b) { …

Enforce Correct Integer Arithmetic using the C++ Safe Numerics Library

The Boost Safe Numerics library, created by Robert Ramey aims to enforce correct mathematical operations with the C++ language. Why are safer numeric operations needed? C++ inherited the behavior of the integral types (int, unsigned, long, etc.) from the C language, where they were designed to map closely to the underlying processor hardware. The types …