简体   繁体   English

溢出或 Memory 错误 c++

[英]Overflow or Memory error c++

This could be considered a homework question.这可以被认为是一个家庭作业问题。 This problem is wel known: " you have a triangle of numbers and you have to find the greatest sum "这个问题是众所周知的:“ 你有一个三角形的数字,你必须找到最大的和

well no problem, I've made a solution in python some time ago, works flawless.没问题,我前段时间在 python 中做了一个解决方案,完美无瑕。 But now in c++, the solution is 75256, my answer is 9729. So the problem is that the type short overflows.但是现在在c++中,解是75256,我的答案是9729。所以问题是类型short溢出了。

So to fix this, I assumed changing my array to type int would solve everything.. but then, when declaring an array a[1001][1001] it freezes (i guess memory error).因此,为了解决这个问题,我假设将数组更改为 int 类型可以解决所有问题。但是,当声明数组a[1001][1001]时,它会冻结(我猜是 memory 错误)。

anyone know what to do?有谁知道该怎么做? I tried something with another int, and whenever the value in a got bigger than 32767 it would increment, but my solution then still is off 300?我用另一个 int 尝试了一些东西,每当a中的值大于 32767 时,它就会增加,但我的解决方案仍然是 300? (the code works - tested on many smaller ones) (代码有效 - 在许多较小的代码上测试)

#include <iostream>
#include <fstream>

int main() {
    std::ofstream fout ("numtri.out");
    std::ifstream fin  ("numtri.in");
    short trifield[1001][1001] = {0};
    int Rows, tmp=0;
    fin >> Rows;
    for (int x = 0; x<Rows;x++) 
        for (int nr = 0; nr<=x;nr++){
            fin >> tmp;
            trifield[x][nr] = tmp;}

    for (int y = (Rows-2); y > -1; y--)
        for (int x = 0; x <= y+1; x++) {
            int a = trifield[y+1][x];
            int b = trifield[y+1][x+1];
            if (a > b) trifield[y][x] += a;
            else       trifield[y][x] += b;
        }
    fout << trifield[0][0] << std::endl;
    return 0;    
}

note: I'm not looking for the solution, just a good way to deal with overflows, examples appreciated!注意:我不是在寻找解决方案,只是一种处理溢出的好方法,示例赞赏!

If you have issues with memory try to assign your array dynamically:如果您对 memory 有疑问,请尝试动态分配您的数组:

short** trifield = new short[1001][1001];

You have an array of 1001x1001 shorts... that's 1002001*2 bytes.你有一个 1001x1001 短裤的数组......那是 1002001*2 字节。 That's all going on your local stack .这一切都在您的本地堆栈上进行。 Depending on your system that could be TOO big.根据您的系统可能太大。 Try allocating the space for your 'trifield' with a malloc instead.尝试使用 malloc 为您的“三场”分配空间。 See what that does for you看看这对你有什么作用

You get a stack overflow instead of a numeric overflow!您会得到堆栈溢出而不是数字溢出!

Move the array to static memory outside of main, so it doesn't use the stack.将数组移动到主外部的 static memory ,因此它不使用堆栈。

The way I check for overflow is to check for an obviously bogus result.我检查溢出的方法是检查明显虚假的结果。 For instance,例如,

if (myInt + 1 < myInt) {
    // Overflow condition
    cerr << "Overflow" << endl;
}
else {
    myInt++;
}

Overflowing an int is an UB.溢出一个 int 是一个 UB。 Overflowing an unsigned int value is defined in the standard.溢出无符号整数值在标准中定义。

So, the only way is to manually check the values before doing the operation, and make sure it doesn't overflow.因此,唯一的方法是在执行操作之前手动检查值,并确保它不会溢出。

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

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