Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does integer overflow cause errors with C++ iostreams?

Ok, so I have some problems with C++ iostreams that feels very odd, but it is probably defined behaviour, considering this happens with both MSVC++ and G++.

Say I have this program:

#include <iostream>
using namespace std;

int main()
{
   int a;
   cin >> a;
   cout << a << endl;
   cin >> a;
   cout << a << endl;

   return 0;
}

If I intentionally overflow by giving the first cin a value that is larger than the max limit of an int, all further calls to cin.operator>>() will immediately return for some reason, and a is set to some value. The value seems to be undefined.

Why, and where is this behavior documented? Is there a way to figure out if such an overflow occured?

Also, this similar program seems to work as I intend. If I overflow the value, it will give a some value, and continue on as if the overflow never happened.

#include <cstdio>
using namespace std;

int main()
{
   int a;
   scanf("%d", &a);
   printf("%d\n", a);
   scanf("%d", &a);
   printf("%d\n", a);
   scanf("%d", &a);
   printf("%d\n", a);

   return 0;
}
like image 747
Maister Avatar asked Aug 27 '10 08:08

Maister


People also ask

What happens when integer overflow in C++?

Integers in C++ are allocated with a certain number of bits. If an integer value, takes more bits than the allocated number of bits, then we may encounter an overflow or underflow. The integer overflow occurs when a number is greater than the maximum value the data type can hold.

What happens if you 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.

How does C handle int overflow?

An integer overflow occurs when you attempt to store inside an integer variable a value that is larger than the maximum value the variable can hold. The C standard defines this situation as undefined behavior (meaning that anything might happen).

What happens when a signed integer overflows?

In contrast, the C standard says that signed integer overflow leads to undefined behavior where a program can do anything, including dumping core or overrunning a buffer. The misbehavior can even precede the overflow. Such an overflow can occur during addition, subtraction, multiplication, division, and left shift.


1 Answers

iostreams is designed to detect errors and enter an error state. You get the same result from integer overflow as from entering a non-numeric string.

Cast cin (or any stream) to bool or check cin.rdstate() to determine if an error has occurred.

Call cin.clear() and cin.ignore() to flush out the error. It will pick up at the point of the characters that failed.

As for the official documentation, the Standard unfortunately gets a bit inscrutable in the bowels of iostreams. See §27.6.1.2.1, 27.6.1.2.2, and 22.2.2.1.1/11 (no kidding):

— The sequence of chars accumulated in stage 2 would have caused scanf to report an input failure. ios_base::failbit is assigned to err.

The documentation for scanf is just as impenetrable, and I'll take it on faith that overflow is supposed to be an error.

like image 92
Potatoswatter Avatar answered Nov 10 '22 01:11

Potatoswatter