Is overflow_error detected at runtime in C ++ when an integer variable cannot contain a value that is not expected

I am learning C ++, I was trying to write this function to find the largest fibonacci integer that can fit into an integer type:

void findFibThatFitsInAnInt() { int n1 = 1; int n2 = 1; int fib = 0; try { while ( true ) { fib = n1 + n2; n1 = n2; n2 = fib; cout << "Fibonacci number : " << fib << "\n"; } } catch (overflow_error & e) { cout << "The largest fib that can fit into an int is : " << fib << "\n"; cout << e.what() << "\n"; } cout << "The largest fib that can fit into an int is : " << n1 << "\n"; } 

But the fact is that overflow_error is not caught at all. I know other ways to do this:

I know that I can write like:

 while ( fib >= 0 ) { fib = n1 + n2; n1 = n2; n2 = fib; cout << "Fibonacci number : " << fib << "\n"; } 

and because fib is just "int" and not an unsigned int, it will eventually become <0 (oddly enough) when it is assigned a value that exceeds the capacity of type int.

Question: overflow_error for such a bandwidth issue detected at runtime in C ++? Didn't I understand something about overflow_error? This is what I know from my google foo:

Defines the type of object to be excluded. It can be used for arithmetic errors of report overflow (that is, situations when the result of the calculation is too large for the target type)

If overflow_error is ignored for whole overflows, is there a way to enable it for my C ++ compiler (visual studio 2013?)

+5
source share
3 answers

Short answer: None. Neither C nor C ++ will automatically detect integer overflows at runtime.

More on C ++ std :: overflow_error:

http://en.cppreference.com/w/cpp/error/overflow_error

std::overflow_error Defined in the header <stdexcept>

Defines the type of object to be excluded. It can be used for arithmetic errors of report overflow (that is, situations when the result of the calculation is too large for the target type)

The only standard library components that throw this exception are: std :: bitset :: to_ulong and std :: bitset :: to_ullong.

The math functions of the standard library components do not throw this exception (math functions report overflow errors as specified in math_errhandling). However, third-party libraries use this. For example, boost.math throws std :: overflow_error if boost :: math :: policy :: throw_on_error is on (default setting).

Here is more detailed information about what you can do in your code to detect and handle integer overflows:

How to detect integer overflow?

+6
source

According to the standard (emphasis mine):

5 Expressions [expr]
....

If during the evaluation of the expression the result is not determined mathematically or if there are no representable values ​​for the type in the range, the behavior is undefined . [Note: most existing C ++ implementations ignore whole overflows .

PS By the way, note that

3.9.1 Basic types [basic.fundamental]
....

(footnote)
unsigned arithmetic is not overflowed , because a result that cannot be represented as a result of an unsigned integer type decreases modulo a number that is greater than the largest value that can be represented by an unsigned integer type.

+6
source

is overflow_error for this type of bandwidth issue detected at runtime in C ++?

No, as from the documentation

The only standard library components that throw this exception are std::bitset::to_ulong and std::bitset::to_ullong .

Overflows caused by internal math operations will not be caught and will simply leave you with unexpected results. The exact behavior is not defined in the standard ( excellent quotes in the @AlexD answer).

+4
source

Source: https://habr.com/ru/post/1242030/


All Articles