对于特定情况的整数溢出似乎是由整数溢出引起的错误



我是本网站的新手。我四处寻找类似于我的问题,但没有发现,所以我自己问一个新问题。我希望我正确格式化。

我正在编写的程序的一部分总结了一些大量数字。这是我目前拥有的代码:

Ball* tmp = player2->RemoveMaxNode();
if (!tmp->GetChosen())
{
    cout << player2->GetHeapType() << " is chosing " << tmp->GetValue() <<  "-" << tmp->GetSumOfDigits() <<endl;
    cout << "Adding " << tmp->GetValue() << " to ";
    cout << player2->GetScore();
    cout << " which should = " << tmp->GetValue() + player2->GetScore() << endl;
    player2->UpdateScore(tmp->GetValue());
    cout << player2->GetHeapType()<< " score now is: ";
    cout << player2->GetScore();
    cout << endl;
    tmp->SetChosen(true);
    numberOfBalls--;
    j++;
    // cout << endl;
}

使用我的updatesCore((函数为:

void Heap::UpdateScore(unsigned long int value)
{
    this->currentScore += value;
}

这将使我输出:

AGENT1 is chosing 937504347-42
Adding 937504347 to 0 which should = 937504347
AGENT1 score now is: 937504347
AGENT1 is chosing 709551656-44
Adding 709551656 to 937504347 which should = 1647056003
AGENT1 score now is: 1647056003
AGENT1 is chosing 681463104-33
Adding 681463104 to 1647056003 which should = 2328519107
AGENT1  score now is: 2328519107
AGENT1 is chosing 672306410-29
Adding 672306410 to 2328519107 which should = 3000825517
AGENT1 score now is: 3000825517
AGENT1 is chosing 667082001-30
Adding 667082001 to 3000825517 which should = 3667907518
AGENT1 score now is: 3667907518
AGENT1 is chosing 378250713-36
Adding 378250713 to 3667907518 which should = 4046158231
AGENT1 score now is: 4046158231
AGENT1 is chosing 309421734-33
Adding 309421734 to 4046158231 which should = 60612669
AGENT1 score now is: 60612669
AGENT1 is chosing 206733105-27
Adding 206733105 to 60612669 which should = 267345774
AGENT1 score now is: 267345774
AGENT1 is chosing 151431905-29
Adding 151431905 to 267345774 which should = 418777679
AGENT1 score now is: 418777679
AGENT1 is chosing 13048925-32
Adding 13048925 to 418777679 which should = 431826604
AGENT1 score now is: 431826604

Note :破折号后的整数值是仪表板之前值的数字之和。这可以忽略。我经过并手动计算了所有这些,它们都是正确的,直到到达

AGENT1 is chosing 309421734-33
Adding 309421734 to 4046158231 which should = 60612669
AGENT1 score now is: 60612669

此加法的结果数应为4,355,579,965,而是60612666。这两个数字的差异为4,294,967,296,我知道这是2^32位整数的最大尺寸。因此,我将所有变量更改为未签名的长时间。tmp的值是未签名的长INT,player2的分数也是如此。但是,问题仍然存在,我不知道我能做什么。我认为这是一个整数溢出错误,所以我硬编码了所有要添加的整数值,并且得到了溢出错误。

main.cpp: In function 'int main(int, char**)':
main.cpp:147:53: warning: integer overflow in expression [-Woverflow]
         unsigned long int asdf = 937504347+709551656+681463104+672306410+667082001+378250713+309421734+206733105+151431905+13048925;
                                  ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~

但是,对于我的实际程序来说,情况并非如此,因为我会收到一个错误,告诉我这是溢出,对吗?我不知道在这里做什么,我尝试了我能想到的一切。怎么了?

谢谢。

关于:

的语句
unsigned long int asdf = 937504347 + 709551656;

int 937504347709551656添加共同创建int结果,然后加载到unsigned long变量中。为了避免在int -ADD阶段溢出,您应该已经使用了正确的类型,例如:

unsigned long int asdf = 937504347UL + 709551656UL;

这似乎已经知道,因为您会遇到编译时间错误,因为编译器已经知道您要添加的值。

但是,您的争论是,与编译器在编译时不知道的任意值相同的结果是不正确的。它只会添加值和滚动未签名值。它可能还会签署签名值,但是从技术上讲,这是不确定的行为,因此如果格式化硬盘格式化: - (

,请不要感到惊讶

在任何情况下,标准仅保证较高等级的类型具有更大的或等于范围较低的等级。unsigned long仅是所需的为32位(更正确地,具有32位可以表示的范围(。如果需要保证的尺寸,请使用cstdint中的uintX_t类型。uint64_t类型应大大扩展您的范围,可保证您的最高价值约为18千亿(18,446,744,073,709,551,615(。

最新更新