Catching unsigned int overflow: C++ detective from zero to pro

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

A trap for the inattentive: why is unsigned int a time bomb?

\n\n

Every C++ developer sooner or later encounters a situation where numbers behave like angry rabbits — endlessly jumping from positive to zero. We are talking about unsigned int overflow. Unlike its signed counterpart (signed int), unsigned overflow in C++ is strictly defined by the standard — it is not UB (undefined behavior). The number simply wraps around modulo 2N. But this does not make the error any less fatal.

\n\n

Imagine: you are calculating an account balance, the number of elements in an array, or a timer's runtime. One accidental overflow — and the application logic collapses. The question in the title "How do I detect unsigned integer overflow?" gathered thousands of views on Stack Overflow for a reason. Let's analyze three approaches to overflow detection: from naive (but working) to professional.

\n\n

Why don't standard checks work?

\n\n

The first thing that comes to a beginner's mind:

\n\n
unsigned int a = UINT_MAX;\nunsigned int b = 1;\nif (a + b < a) // Naive check\n    std::cout << "Overflow!";
\n\n

This code works, but only for addition. For multiplication, subtraction, or mixed types, the logic breaks. Additionally, the compiler may consider such a check redundant and remove it during optimization. Especially if the types are signed. Don't rely on magic — write explicitly.

\n\n

Approach #1: Pre-operation arithmetic (Pre-check)

\n\n

The most reliable way is to check for the possibility of overflow before performing the operation. For adding two unsigned numbers, the condition is simple:

\n\n

If a + b can overflow, then a > (UINT_MAX - b).

\n\n
#include <climits>\n#include <iostream>\n\nbool will_add_overflow(unsigned int a, unsigned int b) {\n    return a > UINT_MAX - b;\n}\n\nint main() {\n    unsigned int x = 4000000000;\n    unsigned int y = 500000000;\n    \n    if (will_add_overflow(x, y)) {\n        std::cerr << "Error: overflow during addition!\\n";\n    } else {\n        std::cout << "Result: " << x + y << "\\n";\n    }\n    return 0;\n}
\n\n

Multiplication — more complex

\n

For multiplication, the check is slightly trickier: a > UINT_MAX / b (and don't forget about division by zero).

\n\n
bool will_mult_overflow(unsigned int a, unsigned int b) {\n    if (b == 0) return false; // Multiplication by zero is safe\n    return a > UINT_MAX / b;\n}
\n\n

Pros: Cross-platform, full control, no hidden dependencies.
\nCons: Need to write separate functions for each operation. The code becomes cumbersome.

\n\n

Approach #2: Post-operation check (Post-check)

\n\n

The C++ standard guarantees that for unsigned types, the result of an operation is (a + b) mod 2^N. Therefore, we can perform the operation and then check if the value has "wrapped around".

\n\n
#include <iostream>\n#include <limits>\n\ntemplate<typename T>\nbool add_overflow(T a, T b, T& result) {\n    result = a + b;\n    return result < a; // Key trick\n}\n\nint main() {\n    unsigned int res;\n    if (add_overflow(4000000000u, 500000000u, res)) {\n        std::cout << "Overflow detected!\\n";\n    } else {\n        std::cout << "OK: " << res << "\\n";\n    }\n    return 0;\n}
\n\n

Important: This trick works only if a and b are of the same unsigned type. For uint32_t — excellent. For a mix of unsigned int and int — failure.

\n\n

Trap for subtraction

\n

For subtraction, the condition is reversed: result > a.

\n\n
template<typename T>\                

Blogs

Book Recommendations