使用iostream和指针获取Seg Fault

Ath*_*ius 1 c++

我想读取数据文件(迷宫),其中我似乎已经编写自己一个赛格错,我承认我是病了我的有关动态分配的大学讲座,并搜查了我的问题throughly,无济于事.这是我的代码片段:

void MazeClass::ReadMaze(ifstream& mazedata) {
    mazedata >> row >> column;  // Pulls the size from the file
    GetExit(mazedata);          // Does the same for above, just for the Exit (Required in Class) 
    GetEntrance(mazedata);      // Does the same, just for the entrance (Required in Class)
    maze = new char*[row];      // First array of pointers for 2d array
    for (unsigned i; i<row;i++)
    {                           // Creates the second set of arrays 
        maze[i]=new char[column];
    }
    for (int y=0;y<column;y++)
    {                           // Keeping the maze inside boundries (step 1)
        for (int x=0;x<row;x++) // (Step 2)
        {
            maze[x][y]=mazedata.get(); // <--- Here is where my Seg Fault happens.
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是gdb告诉我的:

程序接收信号SIGSEGV,分段故障.0x08048fe9在MazeClass :: ReadMaze(这= 0xbffff524,mazedata = ...)在MazeClass.cpp:36 36迷宫[X] [Y] = mazedata.get();

提前感谢您提供所有帮助.

既然我的代码是由一个愚蠢的错误修复的,我现在能够继续讨论下一个问题:

(gdb) run
Starting program: /home/athetius/projects/code/netbeans/OLA4/a.out 
Please Enter Data Filename: MyMaze2.dat


**************12142*********** ***12142*
*            12142***** *     *  12142 *
*           12142 ************  12142***
***   *    12142           ****12142****
*         12142               12142    *
*    ****12142*****   ** *   12142     *
*       12142        *      12142 * *  *
*      12142        *******12142***    *
*     12142*        ** ***12142*********
*    12142               12142          
*   12142  *************12142***       *
*  12142               12142   *****  **
**12142*************  12142 *          *
*12142       ******* 12142            **
12142***************12142
Program exited normally.
Run Code Online (Sandbox Code Playgroud)

输出:查看MyMaze2.dat为:

************************* ****
*            ***** *     *   *
*            ************  ***
***   *               ********
*                            *
*    *********   ** *        *
*               *       * *  *
*              **********    *
*     *        ** ************
*                             
*     ****************       *
*                    *****  **
***************   *          *
*       *******             **
******************************
Run Code Online (Sandbox Code Playgroud)

dap*_*wit 5

for (unsigned i; i<row;i++)开始第一个for循环的行中,您不进行初始化i.试试unsigned i=0;.这可能无法解决所有问题,但它的开始:)