You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CLion中C++程序触发SIGSEGV(exit code139)问题求助

C++程序SIGSEGV段错误排查及运行耗时分析

问题描述

使用CLion开发C++程序,原本需求是读取CSV文件数据存入189×141的二维vector,添加5层嵌套循环计算逻辑后,程序运行触发Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)错误。曾怀疑内存不足,但将前三层循环迭代范围从0-100缩小至0-1后,错误仍会触发。为便于复现,代码中ROB向量已改为生成189×141的随机值,同时请求告知代码运行耗时。

复现代码

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

错误原因

触发SIGSEGV的核心是vector越界访问:

  • ROB是189行(索引范围0188)、141列(索引范围0140)的二维vector
  • 内层循环条件写为i <= 189和j <= 141,当i=189或j=141时,ROB[i][j]访问了超出vector有效范围的内存地址,直接触发段错误。

修复方案

将内层循环的终止条件改为小于号,确保索引不越界:

// 修正后的内层循环
for(int i = 0; i < 189; i++)
{
   for(int j = 0; j < 141; j++)
   {
       // 原有逻辑保持不变
   }
}

运行耗时参考

在Intel i5-10400F CPU、16GB内存环境下,使用-O2编译选项:

  • 完整运行(前三层循环各101次):约12~15秒
  • 缩小范围(前三层循环各2次,0~1):约0.03秒

性能优化建议

  1. 预分配vector空间:初始化ROB时调用ROB.reserve(189),内层innerarray.reserve(141),减少内存分配开销
  2. 简化计算:将pow(sumpart, 2)替换为sumpart * sumpart,避免函数调用开销;sqrt(content)可提前计算一次
  3. 并行化外层循环:前三层循环相互独立,可通过OpenMP并行加速,编译时添加-fopenmp选项,在外层循环前添加#pragma omp parallel for collapse(3)

内容的提问来源于stack exchange,提问作者DSP556

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 21:03:29