溢出或内存错误 c++

发布于 2024-11-09 06:07:29 字数 1251 浏览 0 评论 0原文

这可以被视为一个家庭作业问题。 这个问题是众所周知的:“你有一个三角形数字,你必须找到最大的和

好吧,没问题,我前段时间用 python 做了一个解决方案,工作完美。 但现在在c++中,解是75256,我的答案是9729。 所以问题是类型 short 溢出。

因此,为了解决这个问题,我假设将数组更改为 int 类型可以解决所有问题。但是,当声明数组 a[1001][1001] 时,它会冻结(我猜是内存错误)。

有人知道该怎么办吗? 我用另一个 int 尝试了一些东西,每当 a 中的值大于 32767 时,它就会增加,但我的解决方案仍然是 300? (代码有效 - 在许多较小的代码上进行了测试)

#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;    
}

注意:我不是在寻找解决方案,只是一个处理溢出的好方法,示例值得赞赏!

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.
But now in c++, the solution is 75256, my answer is 9729.
So the problem is that the type short overflows.

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).

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? (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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拔了角的鹿 2024-11-16 06:07:29

如果您遇到内存问题,请尝试动态分配数组:

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

If you have issues with memory try to assign your array dynamically:

short** trifield = new short[1001][1001];
非要怀念 2024-11-16 06:07:29

您有一个 1001x1001 短裤数组...即 1002001*2 字节。这一切都在您的本地堆栈上进行。根据您的系统,可能太大了。尝试使用 malloc 为“trifield”分配空间。看看这对你有什么作用

You have an array of 1001x1001 shorts... that's 1002001*2 bytes. 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. See what that does for you

一场信仰旅途 2024-11-16 06:07:29

您会得到堆栈溢出而不是数字溢出!

将数组移动到 main 之外的静态内存,这样它就不会使用堆栈。

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.

盛夏尉蓝 2024-11-16 06:07:29

我检查溢出的方法是检查明显虚假的结果。例如,

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

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++;
}
樱桃奶球 2024-11-16 06:07:29

int 溢出就是 UB。标准中定义了 unsigned int 值的溢出。

因此,唯一的方法是在执行操作之前手动检查值,并确保它不会溢出。

Overflowing an int is an 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文