automatic overflow detection in C++?

c++, overflow

Solution

There may not be much that you would get from standard C++:

5 Expressions

4 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. [ Note: most existing implementations of C++ ignore integer overflows. Treatment of division by zero, forming a remainder using a zero divisor, and all floating point exceptions vary among machines, and is usually adjustable by a library function. —end note ]

Your best bet is to probably use the standard fixed width integer types defined in `<cstdint>` such as `uint32_t`.

Take a look at the `<cerrno>` header too for error codes such as `EOVERFLOW`. There are then the `overflow_error`/`underflow_error` classes from `<stdexcept>`.

Problem

Possible Duplicate: Best way to detect integer overflow in C/C++ Oftentimes when I've coded something in C++ using large numbers I can't tell when overflow is occurring, even if I am using something like a long long or other 64-bit data type. Is there an effective way to detect when overflow is occurring than witnessing erroneous values?

Original source

Related problems