简体   繁体   English

大文件的C ++浮点异常

[英]C++ Floating Point Exception with large files

While executing the following code with a large amount of data (roughly 2MB), I am receiving a Floating Point Exception on line 21. 在执行以下具有大量数据(大约2MB)的代码时,我在第21行接收到一个浮点异常。

    #include <bits/stdc++.h>
using namespace std;


int main() {
    std::ios_base::sync_with_stdio(false);
    int S, Q, type, seq, pos;
    int x, y, lastAnswer = 0;
    cin >> S >> Q;
    vector< vector <int> > seqList(S);

    for(int i=0; i<Q; i++){
        cin >> type >> x >> y;
        if( type == 1 ){
            seq = (x^lastAnswer) % S;
            seqList[seq].push_back(y);
            if(seq == 0) lastAnswer = 0;
        }
        else if( type == 2 ){
            seq = (x^lastAnswer) % S;
            pos = (int)(y % seqList[seq].size()); //Line 21
            lastAnswer = seqList[seq][pos];
            cout << lastAnswer << endl;
        }
    }

    return 0;
}
pos = (int)(y % seqList[seq].size()); //Line 21

seqList[seq].size() is 0 . seqList[seq].size()0 @Lưu Vĩnh Phúc @LưuVĩnhPhúc

With a%b , if b == 0 then the behavior is not defined. 对于a%b ,如果b == 0则未定义行为。 Many platforms report a floating point exception when an integer division or % by zero is attempted. 当尝试整数除法或%除以零时,许多平台都会报告浮点异常。

Re-write to to detect when seqList[seq].size() == 0 . 重写以检测seqList[seq].size() == 0

Also check S == 0 as it is used with (x^lastAnswer) % S @Jeremy Friesner 还要检查S == 0因为它与(x^lastAnswer) % S @Jeremy Friesner一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM