首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cLion C++“处理完成出口代码139 (被信号11: SIGSEGV中断)”

cLion C++“处理完成出口代码139 (被信号11: SIGSEGV中断)”
EN

Stack Overflow用户
提问于 2022-07-25 02:33:14
回答 1查看 271关注 0票数 0

刚刚开始使用cLion作为c++的ide。我试图运行这段代码,其中它读取csv文件并将值存储在一个189 x 141 2d向量中。然后,它遍历5次循环。所有操作都很顺利,直到我包含了5个嵌套的for循环;此时,我看到了执行时的“使用退出代码139 (被信号11: SIGSEGV中断)完成的进程”消息。我看到了一个类似问题的答案,声称这是我的电脑内存不足的结果。情况会是这样吗?

当前3个for循环有ib0、ib1、ib2在0到100之间时,这当然意味着总共有超过260亿次迭代。但是,当我将这个范围缩小到0到1时,我仍然会收到消息。

为了重现性,我只知道ROB向量是189 x 141随机值。

编辑:如果有人运行这段代码,请告诉我需要多长时间?

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    double innerarrayval;
    vector<vector<double>> ROB;
    vector<double> innerarray;
    for(int x=0;x<189;x++) {
        innerarray.clear();
        for (int y = 0; y < 141; y++) {
            innerarrayval = rand() % 1000;
            innerarray.push_back(innerarrayval);
        }
        ROB.push_back(innerarray);
    }

    double b0,b1,b2;
    int nnb;
    double chisquared, amean, content, sumpart, sumpartsquared,chisquaredNDOF,chimin;
    double b0mem, b1mem, b2mem;
    chimin = 1000.0;

    for(int ib0 = 0; ib0 < 101; ib0++)
    {
        b0 = 15.0 + 0.1 * (ib0 - 1);
        for(int ib1 = 0; ib1 < 101; ib1++)
        {
            b1 = -0.002 * (ib1 - 1);
            for(int ib2 = 0; ib2 < 101; ib2++)
            {
                b2 = 0.002 * (ib2 - 1);
                nnb = 0;
                chisquared = 0;
                amean = 0;
                for(int i = 0; i <= 189; i++)
                {
                   for(int j = 0; j <= 141; j++)
                   {
                       if((i >= 50 and i <= 116) and (j >= 42 and j <= 112))
                       {
                          continue;
                       }
                       else
                       {
                           content = ROB[i][j];
                           if(content == 0)
                           {
                               content = 1;
                           }
                           amean = amean + content;
                           sumpart = (content - b0 - (b1 * i) - (b2 * j))/sqrt(content);
                           sumpartsquared = pow(sumpart, 2);
                           chisquared = chisquared + sumpartsquared;
                           nnb = nnb + 1;
                       }
                   }
                }
                chisquaredNDOF = chisquared/double(nnb);
                amean = amean/double(nnb);
                if(chisquaredNDOF < chimin)
                {
                    chimin = chisquaredNDOF;
                    b0mem = b0;
                    b1mem = b1;
                    b2mem = b2;
                }
            }
        }
    }
    cout<<"chi squared: "<<chimin<<"\n";
    cout<<"b0: "<<b0mem<<"\n";
    cout<<"b1: "<<b1mem<<"\n";
    cout<<"b2: "<<b2mem<<"\n";
    cout<<"mean: "<<amean<<"\n";
    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-25 02:54:24

代码语言:javascript
复制
    for(int x=0;x<189;x++) {
        innerarray.clear();
        for (int y = 0; y < 141; y++) {
            innerarrayval = rand() % 1000;
            innerarray.push_back(innerarrayval);
        }
        ROB.push_back(innerarray);
    }

这部分初始化循环小心,小心翼翼,初始化了二维ROB矢量.正如每个for循环的极限所指出的,第一维的有效索引是0-188,第二维是0-140。

这是正确的。在C++数组/向量索引中,从0开始,因此对于"189 x 141 2d向量“的预期结果,第一个索引的值范围为0-188,第二个索引的范围为0-140。

如果你在你的代码中更进一步,到读取二维矩阵的部分:

代码语言:javascript
复制
                for(int i = 0; i <= 189; i++)
                {
                   for(int j = 0; j <= 141; j++)
                   {

因为这使用了<=,这将尝试访问ROB中的值,其一维索引范围为0-189,而第2维索引范围为0-141。

越界访问会导致未定义的行为,以及您可能的崩溃。

您应该将这个明显的bug作为学习如何使用调试器的一个极好的机会。如果您使用调试器运行此程序,调试器将在崩溃时停止。如果然后使用调试器检查所有变量的值,我预计ij的超出范围值将非常明显,从而使bug变得清晰。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73103528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档