Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is overflow_error caught at runtime in C++ when a integer variable cannot hold a value that it is not supposed to

Tags:

c++

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 thing is overflow_error is not caught at all. I know other ways of doing 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 an "int" not an unsigned int, it will eventually become < 0 ( strangely enough ) when it is assigned a value that is larger than the capacity of int type.

Question is : is overflow_error for this kind of capacity issue caught at runtime in C++? Did I misunderstand something about overflow_error? This is what I know from my google foo:

Defines a type of object to be thrown as exception. It can be used to report arithmetic overflow errors (that is, situations where a result of a computation is too large for the destination type)

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

like image 322
SomeDude Avatar asked Feb 01 '16 19:02

SomeDude


People also ask

What happens when integer overflow in C?

Overview. Integer Overflow is a phenomenon that occurs when the integer data type cannot hold the actual value of a variable. Integer Overflow and Integer Underflow in C, do not raise any errors, but the program continues to execute (with the incorrect values) as if nothing has happened.

What happens when int overflows?

An integer overflow is a type of an arithmetic overflow error when the result of an integer operation does not fit within the allocated memory space. Instead of an error in the program, it usually causes the result to be unexpected.

How can you tell if a signed integer is overflow?

Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in “result” and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.

How does division cause integer overflow?

Division. Division is between two operands of arithmetic type. Overflow can occur during two's complement signed integer division when the dividend is equal to the minimum (negative) value for the signed integer type and the divisor is equal to −1 . Division operations are also susceptible to divide-by-zero errors.


3 Answers

Short answer: No. Neither C nor C++ will automatically detect integer overflow at runtime.

Here are more details about C++ std::overflow_error:

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

std::overflow_error Defined in header <stdexcept>

Defines a type of object to be thrown as exception. It can be used to report arithmetic overflow errors (that is, situations where a result of a computation is too large for the destination type)

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

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

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

How to detect integer overflow?

like image 77
paulsm4 Avatar answered Sep 25 '22 03:09

paulsm4


According to the standard (emphasis mine):

5 Expressions [expr]
....

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.

P.S. BTW, please note that

3.9.1 Fundamental types [basic.fundamental]
....

(footnote)
unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

like image 30
AlexD Avatar answered Sep 27 '22 03:09

AlexD


is overflow_error for this kind of capacity issue caught 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 as induced by intrinsic mathematical operations won't be caught, and just leave you with unexpected results. The exact behavior isn't defined in the standard (great cite in @AlexD 's answer).

like image 33
πάντα ῥεῖ Avatar answered Sep 27 '22 03:09

πάντα ῥεῖ