C ++代码因“分段错误”而失败,但我不知道为什么

问题描述 投票:0回答:1

我已经尝试查找问题大约一个星期了,但是没有成功。该代码应整理出由另一个工作脚本生成的点云的“外壳”(请参见代码注释)。输入文件中的所有点都位于3维均匀间隔的网格上。

#include <iostream>
#include <cmath>
#include <fstream>
#include <regex>

using namespace std;

/* This script is supposed to take a pointcloud and remove all points except those that lie 
 * on the hull:
 * 
 * .... ...             .... ...
 * ...........          .  ... ...
 * ............   ==>   .       ...
 * ..........           ..      .
 *  .........            ........
 * 
 * This is done by taking every point 'P' and checking in a 3x3x3 grid around it if there are other 
 * points '0'. If the amount of empty grid spaces is more or equal to one then P is on the hull.
 * 
 * not in hull:     in hull:        in hull:
 *    0 0 0           0   0                
 *    0 P 0           0 P 0             P 0
 *    0 0 0           0 0 0           0 0 0
*/

int main(){

    //Settings:
    string mainfilename = "Mandelbulb.xyz";
    string hullfilename = "Hull_" + mainfilename;
    const double radiusdivider = 10.0;

    //Variables for later use:
    double xmain, ymain, zmain;
    double xmain2, ymain2, zmain2;
    double xcheck, ycheck, zcheck;
    double div;
    const double radius = div/radiusdivider;    //'Size' of a point
    int envcount = 0;   //points in the immediate enviroment (3x3x3-grid)
    int hulllinecount = 0;


    //Open file for counting lines:
    ifstream mainfile;
    mainfile.open(mainfilename);
    int linecount = count(istreambuf_iterator<char>(mainfile),
        istreambuf_iterator<char>(), '\n');
    linecount--;    //first line is no coordinate
    mainfile.close();


    //Coordinate arrays:
    double maincoords[3][linecount];
    double hullcoords[3][linecount];    //Has at most 'linecount'-many coordinates


    //Load file to array:
    cout << "Loading " << mainfilename << "... ";
    mainfile.open(mainfilename);
    mainfile >> div;
    for(int i = 0; i < linecount; i++){
        mainfile >> maincoords[0][i];
        mainfile >> maincoords[1][i];
        mainfile >> maincoords[2][i];
    }
    mainfile.close();
    cout << "Done\n";


    //Cycle through every point in maincoords:
    cout << "Checking " << linecount << " points... ";
    for(int i = 0; i < linecount; i++){
        xmain = maincoords[0][i];
        ymain = maincoords[1][i];
        zmain = maincoords[2][i];
        envcount = 0;

        //Cycle through 3x3x3 grid around point
        for(int l = 0; l <= 2; l++){
        for(int m = 0; m <= 2; m++){
        for(int n = 0; n <= 2; n++){
            xcheck = xmain + div - l*div;
            ycheck = ymain + div - m*div;
            zcheck = zmain + div - n*div;

            //Check if grid-point is also in maincoord:
            for(int p = 0; p < linecount; p++){
                xmain2 = maincoords[0][p];
                ymain2 = maincoords[1][p];
                zmain2 = maincoords[2][p];

                //Condition:
                if( (abs(xcheck-xmain2) <= radius) && 
                    (abs(ycheck-ymain2) <= radius) && 
                    (abs(zcheck-zmain2) <= radius) ){
                    envcount++;
                }
            }

        }
        }
        }

        //If one or more surrounding points are empty, the main point is on the hull and saved:
        if(envcount < 27){
            hulllinecount++;
            hullcoords[0][i] = xmain;
            hullcoords[1][i] = ymain;
            hullcoords[2][i] = zmain;
        }
    }
    cout << "Done\n";

    //Writing hull to hullfile:
    cout << "Writing hull to " << hullfilename << " ... ";
    ofstream hullfile;
    hullfile.open(hullfilename);
    for(int i = 0; i < hulllinecount; i++){
        hullfile << hullcoords[0][i];
        hullfile << "   ";
        hullfile << hullcoords[1][i];
        hullfile << "   ";
        hullfile << hullcoords[2][i];
        hullfile << endl;
    }
    hullfile.close();
    cout << "Done\n";
}

我在Ubuntu 18.04 Terminal中使用g++ -g编译代码。执行时出现Segmentation fault (core dumped)。输入ddd a.out并运行代码时,将显示以下消息:

Program received signal SIGSEGV, Segmentation fault.
0x00005555555559bb in main () at hullmaker.cpp:60

但是,我在第60行附近没有发现任何错误。

c++ segmentation-fault point-clouds
1个回答
0
投票

我想linecount没有正确初始化。如果未打开输入文件,则变量可以初始化为0,并且一旦减小该值,则可能试图创建一个负大小的数组。尝试改用const值,然后检查代码是否正常运行。

© www.soinside.com 2019 - 2024. All rights reserved.