C++ C++;康威';s生命游戏-Cygwin分段故障(堆芯倾倒)

C++ C++;康威';s生命游戏-Cygwin分段故障(堆芯倾倒),c++,c,segmentation-fault,cygwin,conways-game-of-life,C++,C,Segmentation Fault,Cygwin,Conways Game Of Life,我是这个论坛的新手,但我真的需要一些帮助。 我一直在设计考平在C++中的生命游戏,并在CiggWin(用MaxFig文件)编译。我不是要求任何人帮我完成这个项目或类似的事情,但我完全被这一部分卡住了。。。 该程序的一个方面是允许用户输入文本文件作为初始网格的映射,而不是使用随机生成的网格。 下面是一个.txt文件格式的示例(数字和“X”纯粹是示例,文件可以是此格式的任何变体): “X”表示有活细菌的空间,“-”表示死空间。 虽然我的程序在cygwin中编译得很好,但当我运行.exe时,我得到了一

我是这个论坛的新手,但我真的需要一些帮助。 我一直在设计考平在C++中的生命游戏,并在CiggWin(用MaxFig文件)编译。我不是要求任何人帮我完成这个项目或类似的事情,但我完全被这一部分卡住了。。。 该程序的一个方面是允许用户输入文本文件作为初始网格的映射,而不是使用随机生成的网格。 下面是一个.txt文件格式的示例(数字和“X”纯粹是示例,文件可以是此格式的任何变体):

“X”表示有活细菌的空间,“-”表示死空间。 虽然我的程序在cygwin中编译得很好,但当我运行.exe时,我得到了一个“分段错误(内核转储)”错误。到目前为止,我已经做了大量的谷歌搜索,但我发现这个错误通常非常特定于它所关注的程序,所以其他解决方案对我没有多大帮助。 我不想用大量的代码向你们发送垃圾邮件,所以我现在只包含loadFile函数。如果您需要更多的代码来帮助,请告诉我,我会立即发布。 以下是到目前为止我在loadFile函数中的内容:

void GamePlay::loadFile(int r, int c, char** newBoard){
  int i;
  //int r;
  //int c;
  //char** newBoard;


  int len;
  int k;

  do{
    r = 0;
    c = 0;
    string filePath;
    cout << "Please enter a file path" << endl;
    cin >> filePath;
    DIR* path = opendir(filePath.c_str());
    string fpath = filePath.c_str();
    dirent* openIn = readdir(path);
    string line;
    string ext = ".txt";
    i = 0;

    while(openIn && i != -1){
        if(openIn->d_type == DT_REG){
            string fileName = openIn->d_name;
            string filePath = fpath + fileName;
            int begExt = fileName.find_last_of(".");
            int endExt = fileName.find_last_of("t");
            string extension = fileName.substr(begExt, endExt);

            ifstream in(filePath.c_str());
            k = 0;

            if(in && ext == extension){
                getline(in,line);
                istringstream(line) >> r;
                getline(in,line);
                istringstream(line) >> c;
                newBoard = new char*[r]; //create multi-d array
                for(int a = 0; a < r; ++a){
                    newBoard[a] = new char[c];
                }

                while(in.good() && i != -1){
                    if(k <= r){
                        if(len == c){
                            for(int g = 0; g < r; ++g){
                                getline(in,line);
                                len = line.size();
                                char* arr = new char[len];
                                memcpy(arr,line.c_str(),len);
                                for(int h = 0; h < c; ++h){
                                    newBoard[g][h] = arr[h];
                                }
                            }
                        }
                        /*if(len == c){
                        //newBoard = len*sizeof(char*);
                        for(int a = 0; a < len; ++a){
                        newBoard[a] = r;
                        memcpy(newBoard[a], line, r);
                        }
                        }*/
                        else{
                            cout << "Your column number does not match your grid." << endl;
                            cout << "Please try again with a new file." << endl;
                            i = -1;
                        }
                    }
                    else{
                        cout << "Your row number does not match your grid." << endl;
                        cout << "Please try again with a new file." << endl;
                        i = -1;
                    }
                    k++;
                }
            }
            else{
                cout << "File invalid. Please try again with a new file." << endl;
                i = -1;
            }
            return;
        }
        else{
            cout << "Invalid file path. Please try again with a new file." << endl;
            i = -1;
        }
    }
  }while(i = -1);
}
void GamePlay::加载文件(int r、int c、char**newBoard){
int i;
//INTR;
//INTC;
//炭**新板;
内伦;
int k;
做{
r=0;
c=0;
字符串文件路径;
cout文件路径;
DIR*path=opendir(filePath.c_str());
字符串fpath=filePath.c_str();
dirent*openIn=readdir(路径);
弦线;
字符串ext=“.txt”;
i=0;
while(openIn&&i!=-1){
如果(openIn->d_type==DT_REG){
字符串fileName=openIn->d_name;
字符串filePath=fpath+fileName;
int begExt=fileName.find_last_of(“.”);
int endExt=fileName.find_last_of(“t”);
字符串扩展名=fileName.substr(begExt,endExt);
ifstream-in(filePath.c_str());
k=0;
if(in&&ext==扩展名){
getline(in,line);
istringstream(线)>>r;
getline(in,line);
istringstream(线)>>c;
newBoard=new char*[r];//创建多维数组
对于(int a=0;a如果(k使用GDB非常简单,那么

gdb [path-to-your-executable]
run
[wait for it to crash]
backtrace
这将告诉您崩溃发生的位置。您还可以使用

break file:line
当它停止时,使用

print [some expression]
要查找有关当前堆栈帧的信息(变量等),请“继续”转到下一个断点

gdb [path-to-your-executable] [process ID]

如果您的程序向终端屏幕输出大量信息,这是很好的(因为否则它会干扰GDB的输出)如果你最初感兴趣的是发现代码中的问题,你不一定需要调试器,我从来没有使用过一个,通常是从程序中获取bug。你可以设置标志并使用C++语句来识别程序有什么问题。乌尔和学院

我在下面的代码中输入了一些cout语句,这样你就知道我在说什么了。你还没有尝试过这个,是吗?显然,你运行程序,看看生成了哪些标志输出。如果出现了一个标志输出,而后面的一个标志输出没有,那么你就缩小了哪一行代码扰乱了你的程序

    void GamePlay::loadFile(int r, int c, char** newBoard){
      int i;
      //int r;
      //int c;
      //char** newBoard;


      int len;
      int k;

      cout << "flag1";

      do{
        r = 0;
        c = 0;
        string filePath;
        cout << "Please enter a file path" << endl;
        cin >> filePath;

        cout << "flag2";

        DIR* path = opendir(filePath.c_str());
        string fpath = filePath.c_str();
        dirent* openIn = readdir(path);
        string line;
        string ext = ".txt";
        i = 0;

        cout << "flag3";

        while(openIn && i != -1){
            if(openIn->d_type == DT_REG){
                string fileName = openIn->d_name;
                string filePath = fpath + fileName;
                int begExt = fileName.find_last_of(".");
                int endExt = fileName.find_last_of("t");
                string extension = fileName.substr(begExt, endExt);

                cout << "flag4";

                ifstream in(filePath.c_str());
                k = 0;

                if(in && ext == extension){
                    getline(in,line);
                    istringstream(line) >> r;
                    getline(in,line);
                    istringstream(line) >> c;
                    newBoard = new char*[r]; //create multi-d array
                    for(int a = 0; a < r; ++a){
                        newBoard[a] = new char[c];
                    }

                    cout << "flag5";

                    while(in.good() && i != -1){
                        if(k <= r){
                            if(len == c){
                                for(int g = 0; g < r; ++g){
                                    getline(in,line);
                                    len = line.size();
                                    char* arr = new char[len];
                                    memcpy(arr,line.c_str(),len);
                                    for(int h = 0; h < c; ++h){
                                        newBoard[g][h] = arr[h];
                                    }
                                }
                            }
                            /*if(len == c){
                            //newBoard = len*sizeof(char*);
                            for(int a = 0; a < len; ++a){
                            newBoard[a] = r;
                            memcpy(newBoard[a], line, r);
                            }
                            }*/
                            else{
                                cout << "Your column number does not match your grid." << endl;
                                cout << "Please try again with a new file." << endl;
                                i = -1;
                            }
                        }
                        else{
                            cout << "Your row number does not match your grid." << endl;
                            cout << "Please try again with a new file." << endl;
                            i = -1;
                        }
                        k++;
                    }
                }
                else{
                    cout << "File invalid. Please try again with a new file." << endl;
                    i = -1;
                }
                return;
            }
            else{
                cout << "Invalid file path. Please try again with a new file." << endl;
                i = -1;
            }
        }
        cout << "flag6";
      }while(i = -1);
    }
void GamePlay::加载文件(int r、int c、char**newBoard){
int i;
//INTR;
//INTC;
//炭**新板;
内伦;
int k;
库特;
getline(in,line);
istringstream(线)>>c;
newBoard=new char*[r];//创建多维数组
对于(int a=0;a谷歌“GDB教程”。第一个链接非常有用。据我所见,您试图打开目录中的每个“.txt”文件,读取所有文件,然后扔掉结果(包括内存泄漏)除了最后一个。我不完全相信这是个好主意。谢谢你。当我使用你的建议时,我能够得到“无效十进制'0x22DBF0'”就在被提示输入文件路径之后。我仍然不知道该怎么办…有什么想法吗?@homeofmatt-似乎是一个Cygwin的东西,看到了吗?是的,我知道你的意思,在过去几年我一直在编程,滥用打印语句而不是调试。不过我还没有在这里尝试过,因为我已经尝试过了绝对不知道我的错误到底在说什么。我会在尝试后发回。好的,我发现错误发生在:dirent*openIn=readdir(path);在评论中,基于此:…整个opendir函数用于打开目录,而不是单个文件。为什么您需要打开整个目录来读取一个文件?是的,我认为path不是目录。好的,是的,但我试图获取文件名。当我输入一个目录时,它可以工作,但会因为“如果”而给我一个错误(openIn->d_type==DT_REG)“检查。我不知道如何获取文件名,以便在传递if语句时不会出现分段错误
    void GamePlay::loadFile(int r, int c, char** newBoard){
      int i;
      //int r;
      //int c;
      //char** newBoard;


      int len;
      int k;

      cout << "flag1";

      do{
        r = 0;
        c = 0;
        string filePath;
        cout << "Please enter a file path" << endl;
        cin >> filePath;

        cout << "flag2";

        DIR* path = opendir(filePath.c_str());
        string fpath = filePath.c_str();
        dirent* openIn = readdir(path);
        string line;
        string ext = ".txt";
        i = 0;

        cout << "flag3";

        while(openIn && i != -1){
            if(openIn->d_type == DT_REG){
                string fileName = openIn->d_name;
                string filePath = fpath + fileName;
                int begExt = fileName.find_last_of(".");
                int endExt = fileName.find_last_of("t");
                string extension = fileName.substr(begExt, endExt);

                cout << "flag4";

                ifstream in(filePath.c_str());
                k = 0;

                if(in && ext == extension){
                    getline(in,line);
                    istringstream(line) >> r;
                    getline(in,line);
                    istringstream(line) >> c;
                    newBoard = new char*[r]; //create multi-d array
                    for(int a = 0; a < r; ++a){
                        newBoard[a] = new char[c];
                    }

                    cout << "flag5";

                    while(in.good() && i != -1){
                        if(k <= r){
                            if(len == c){
                                for(int g = 0; g < r; ++g){
                                    getline(in,line);
                                    len = line.size();
                                    char* arr = new char[len];
                                    memcpy(arr,line.c_str(),len);
                                    for(int h = 0; h < c; ++h){
                                        newBoard[g][h] = arr[h];
                                    }
                                }
                            }
                            /*if(len == c){
                            //newBoard = len*sizeof(char*);
                            for(int a = 0; a < len; ++a){
                            newBoard[a] = r;
                            memcpy(newBoard[a], line, r);
                            }
                            }*/
                            else{
                                cout << "Your column number does not match your grid." << endl;
                                cout << "Please try again with a new file." << endl;
                                i = -1;
                            }
                        }
                        else{
                            cout << "Your row number does not match your grid." << endl;
                            cout << "Please try again with a new file." << endl;
                            i = -1;
                        }
                        k++;
                    }
                }
                else{
                    cout << "File invalid. Please try again with a new file." << endl;
                    i = -1;
                }
                return;
            }
            else{
                cout << "Invalid file path. Please try again with a new file." << endl;
                i = -1;
            }
        }
        cout << "flag6";
      }while(i = -1);
    }