C++ Conway 的生命游戏 - Cygwin 段错误(核心已转储)

标签 c++ c segmentation-fault cygwin conways-game-of-life

我是这个论坛的新手,但我真的需要一些帮助。 我的任务是用 C++ 编写 Conway 的生命游戏,并在 Cygwin 中编译(使用 makefile)。我不是要任何人为我完成程序或类似的东西,但我完全停留在这一部分...... 该程序的一个方面是允许用户输入文本文件作为初始网格的 map ,而不是使用随机生成的网格。 这是 .txt 文件格式的示例(数字和“X”纯粹是为了举例,文件可以是这种格式的任何变体):

5 (rows)
6 (columns)
-----X
X--X--
------
-XX---
------

“X”代表有活细菌的空间,“-”代表死空间。 虽然我的程序通过 cygwin 编译得很好,但当我运行 .exe 时,我得到一个“Segmentation Fault (Core Dumped)”错误。到目前为止,我已经进行了广泛的谷歌搜索,但我发现这个错误通常非常特定于它所关注的程序,因此其他解决方案对我没有太大帮助。 我不想用大量代码向你们发送垃圾邮件,所以我现在只包含我的 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);
}

我也尝试过使用 GDB 进行调试并找到问题所在,但它超出了我的理解范围。可悲的是,我习惯使用的是 GUI,例如 visual studio 和 eclipse。 任何帮助或建议将不胜感激。 谢谢!!!

最佳答案

使用 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 的输出)。

关于C++ Conway 的生命游戏 - Cygwin 段错误(核心已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18819185/

相关文章:

python - Boost.Python + OpenGL 段错误

更改结构字符串

c++ - 如何OPENCV + CUDA + VideoCapture?

c++ - boost 变体对常用方法的简单调用

c# - Windows 2003 上托管 C++ dll 的 FileLoadException

c++ - 使 v8 对象属性和方法对 JS 可见

c - getdate 和 ctime 无法正常工作

c - fscanf 和数字以 native int 格式而非文本格式存储在文件中的问题

c - 如何从 Matlab C Mex 函数获得两个输出?

c - 多维指针数组重新分配导致段错误