cin溢出时来自cin的异常行为 [英] Unexpected behavior from cin when overflowing int

查看:215
本文介绍了cin溢出时来自cin的异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,这里有一些我无法解释其行为的代码.它发布在下面.我查看了为什么整数溢出会导致C ++ iostream错误?,但它并不能真正回答我的问题.

All, I've got some code here that I can't explain the behavior of. It is posted below. I looked at Why does integer overflow cause errors with C++ iostreams?, but it doesn't really answer my question.

#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int x;
    scanf("%d", &x);
    cout << "Value of x = " << x << endl;
    cin >> x;
    cout << "Failure Detected = " << cin.fail() << endl;
    cout << "Value of x = " << x << endl;
    return 0;
}

因此,我希望这段代码要做的是读入一个整数,打印出该整数的值,读入另一个整数(读入相同的变量),然后打印出该整数.如果输入7和2的输入,则它将按预期工作.但是,如果我为第一个和第二个输入都输入2 ^ 31(溢出int乘以1),则第一个输出将显示"x的值= -2147483648",第二个输出将显示"x的值= 2147483647". cin.fail()也将返回true. cin对输入做了什么?我认为,如果cin.fail()为true,则x的值应保持不变.如果不受影响,我希望x的值像正常情况一样溢出(就像scanf一样). cin这是怎么回事?为什么将值限制为整数最大值?

So, what I expect this code to do is read in an integer, print out the value of that integer, read in another integer (into the same variable), and print out that integer. If I enter input of 7 and 2, then it works as expected. However, if I enter 2^31 (overflow int by one) for both the first and second input, then the first output will say "Value of x = -2147483648" and the second output will say "Value of x = 2147483647". cin.fail() will also return true. What is cin doing to the input? I thought that if cin.fail() was true, the value of x should be left unaffected. If not left unaffected, I would expect the value of x to overflow as normal (like scanf does). What's going on with cin here? Why is it capping the value at integer max value?

提前谢谢!

推荐答案

在C ++ 98中,输入失败时变量未更改.如果您尝试输入一个未初始化的变量,这将是一个不利条件.

In C++98 the variable was unchanged when input failed. This was a disadvantage if you try to input to an uninitialized variable.

例如:

int a;
cin >> a;
cout << a;    // UB if input failed!

在以后的标准中,当输入超出该范围时,变量将设置为可能的最大值或最小值.

In later standards the variable will be set to the largest or smallest value possible when the input is outside of that range.

对于operator>>(int& val),标准说[istream.formatted.arithmetic]:

For operator>>(int& val) the standard says [istream.formatted.arithmetic]:

该转换就像由以下代码片段执行一样(使用与 前面的代码片段):

The conversion occurs as if performed by the following code fragment (using the same notation as for the preceding code fragment):

typedef num_get<charT,istreambuf_iterator<charT,traits> > numget;
iostate err = ios_base::goodbit;
long lval;
use_facet<numget>(loc).get(*this, 0, *this, err, lval);
if (lval < numeric_limits<int>::min()) {
  err |= ios_base::failbit;
  val = numeric_limits<int>::min();
} else if (numeric_limits<int>::max() < lval) {
  err |= ios_base::failbit;
  val = numeric_limits<int>::max();
} else
  val = static_cast<int>(lval);
setstate(err);

这篇关于cin溢出时来自cin的异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆