Overflow in C++

Integer Overflow

Integer overflow occurs when an arithmetic operation results in a value exceeding the storage capacity of the integer type. In C++, signed integer overflow leads to undefined behavior.

Example:

#include <iostream>
#include <limits>

int main() {
    int max = std::numeric_limits<int>::max(); // 2,147,483,647 for 32-bit int
    std::cout << "Max int: " << max << std::endl;

    int overflow = max + 1;  // Undefined behavior
    std::cout << "Overflow int: " << overflow << std::endl;

    return 0;
}

Possible Output:

Max int: 2147483647
Overflow int: -2147483648 (depends on the system)

The result wraps around due to two’s complement representation, but since signed integer overflow is undefined behavior, the actual result is unpredictable.

Unsigned Integer Overflow

For unsigned integers, overflow behavior is well-defined and follows modular arithmetic.

Example:

#include <iostream>
#include <limits>

int main() {
    unsigned int max = std::numeric_limits<unsigned int>::max(); // 4,294,967,295 for 32-bit unsigned int
    std::cout << "Max unsigned int: " << max << std::endl;

    unsigned int overflow = max + 1;  // Wraps around to 0
    std::cout << "Overflow unsigned int: " << overflow << std::endl;

    return 0;
}

Output:

Max unsigned int: 4294967295
Overflow unsigned int: 0

Unlike signed integers, unsigned integer overflow is defined behavior in C++.

Floating-Point Overflow

Floating-point overflow occurs when a value exceeds the representable range of the floating-point type. Instead of wrapping around or causing undefined behavior, the result becomes infinity (inf).

Example:

#include <iostream>
#include <limits>

int main() {
    float large = std::numeric_limits<float>::max(); // ~3.4e+38
    std::cout << "Max float: " << large << std::endl;

    float overflow = large * 10.0f; // Becomes infinity (inf)
    std::cout << "Overflow float: " << overflow << std::endl;

    return 0;
}

Output:

Max float: 3.40282e+38
Overflow float: inf

For floating-point types, exceeding the maximum representable value results in inf instead of undefined behavior.

Preventing Overflow

To avoid overflow issues, consider:

  • Checking value limits using std::numeric_limits<T>::max().
  • Using wider data types (long long, double, etc.).
  • Employing safe arithmetic libraries like SafeInt (Microsoft) or boost::multiprecision.
  • Enabling compiler warnings or sanitizers (-fsanitize=undefined in Clang/GCC).

Conclusion

Overflow behavior in C++ differs based on the data type:

  • Signed integer overflow: Undefined behavior.
  • Unsigned integer overflow: Modular arithmetic.
  • Floating-point overflow: Results in inf.

Proper checks and safe practices are necessary to avoid unintended results in arithmetic operations.