我試圖讀取一個數據文件(迷宮),其中我似乎編碼了自己的一個seg故障,我承認我因爲我的學院的演講而生病關於動態分配,並且通過徹底搜索我的問題,無濟於事。 這裏是我的代碼片段:使用iostream和指針獲取Seg Fault
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.
}
}
}
下面是GDB告訴我:
計劃接收信號SIGSEGV,分割過錯。在MazeClass.cpp中的MazeClass :: ReadMaze(this = 0xbffff524,mazedata = ...)中的0x08048fe9: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.
隨着輸出: 視圖MyMaze2.dat之中:
************************* ****
* ***** * * *
* ************ ***
*** * ********
* *
* ********* ** * *
* * * * *
* ********** *
* * ** ************
*
* **************** *
* ***** **
*************** * *
* ******* **
******************************
哈,工作。我忽略了這一點,現在雖然我得到了一些時髦的東西。 – Athetius
如果你開始一個新的問題,你可能會有更多的運氣。當問題沒有迴應時,我認爲它吸引了更多的人。你也可能想發佈你的代碼,實際上打印迷宮......我很高興我能夠幫助。 – dappawit