c++ Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

背景:
比较四个 vector 的 size,得到最小值,然后遍历从四个vector 中取值,放入一个vector 时报错。

参考:
小白提问:C++提示interrupted by signal 11: sigsegv

重新排查,发现vector 越界
代码:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <dirent.h>
#include <iomanip>

std::string image0_dir = "/xxx/camera_0/";
std::string image1_dir = "/xxx/camera_1/";
std::string image2_dir = "/xxx/camera_2/";
std::string image3_dir = "/xxx/camera_3/";




int min(int a, int b)
{
    return a > b ? b : a;
}

bool get_filelist_from_dir(std::string _path, std::vector<std::string>& _files)
{
    DIR* dir = opendir(_path.c_str());
    struct dirent* ptr;
    std::vector<std::string> file;
    while((ptr = readdir(dir)) != NULL)
    {
        if(ptr->d_name[0] == '.')	continue;
        file.push_back(ptr->d_name);
    }
    closedir(dir);
    sort(file.begin(), file.end());
    _files = file;
}

int main()
{
    std::vector<std::string> image0Paths;
    image0Paths.reserve(370);
    std::vector<std::string> image1Paths;
    image1Paths.reserve(370);
    std::vector<std::string> image2Paths;
    image2Paths.reserve(370);
    std::vector<std::string> image3Paths;
    image3Paths.reserve(370);

    get_filelist_from_dir(image0_dir, image0Paths);
    get_filelist_from_dir(image1_dir, image1Paths);
    get_filelist_from_dir(image2_dir, image2Paths);
    get_filelist_from_dir(image3_dir, image3Paths);

    std::sort(image0Paths.begin(), image0Paths.end());
    std::sort(image1Paths.begin(), image1Paths.end());
    std::sort(image2Paths.begin(), image2Paths.end());
    std::sort(image3Paths.begin(), image3Paths.end());

    std::vector<std::vector<std::string>> image_file_Paths;
    std::vector<std::string> temp;
    temp.reserve(4);
    image_file_Paths.resize(370,temp);

//    std::size_t size = image0Paths.size();
//    if (size != image1Paths.size() || size != image2Paths.size() || size != image3Paths.size()) {
//        std::cerr << "Vectors must have equal length\n";
//        return 1;
//    }
    int size = min(image0Paths.size(), image1Paths.size()) > min(image2Paths.size(), image3Paths.size()) ? min(image0Paths.size(), image1Paths.size()) : min(image2Paths.size(), image3Paths.size());
    std::cout << " size : "<< size << std::endl;



    for (std::size_t i = 0; i < size; ++i) {

        std::string elem0 = image0_dir + image0Paths[i].c_str(); //南
        std::string elem1 = image1_dir + image1Paths[i].c_str(); //北
        std::string elem2 = image2_dir + image2Paths[i].c_str(); //西
        std::string elem3 = image3_dir + image3Paths[i].c_str(); //东
        std::cout << "i " << i << std::endl;


        image_file_Paths[i].push_back(elem0);
        image_file_Paths[i].push_back(elem1);
        image_file_Paths[i].push_back(elem2);
        image_file_Paths[i].push_back(elem3);
//        std::cout << " image_file_Paths[i].push_back(elem3); " << i << std::endl;
    }
   return 0;

错误分析:

int size = min(image0Paths.size(), image1Paths.size()) > min(image2Paths.size(), image3Paths.size()) ? min(image0Paths.size(), image1Paths.size()) : min(image2Paths.size(), image3Paths.size());

中间的大于号错了,应为小于号
大于号算出的 size 值不是四个 vector 的最小值,遍历时最小size的那个vector 超过它自己的size时,越界发生错误。
此时应为:

int size = min(image0Paths.size(), image1Paths.size()) < min(image2Paths.size(), image3Paths.size()) ? min(image0Paths.size(), image1Paths.size()) : min(image2Paths.size(), image3Paths.size());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值