C++ Maze Solving algorithm segmentation fault -
the task create randomly generated maze , solve it, issue whenever recursively search maze exit, segmentation fault @ runtime. of cout debugging.
the main file (where error persists)
#include <string> #include <cmath> #include <cstdlib> #include "main.h" #include "maze.h" int main(int argc, char *argv[]) { // process command line arguments , seed random number // generator, if necessary unsigned int seed; if(argc == 4) { seed = static_cast<unsigned int>(atoi(argv[1])); cout << "initializing pseudorandom number generator seed " << seed << endl; srand(seed); } else if(argc > 4) { cout << "improper usage of main\n"; exit(0); } cout << "beginning execution\n"; // first couple of numbers don't seem quite random, // "prime pump" calling rand() twice (void) rand(); (void) rand(); mazetest(); cout << "finishing execution\n"; return 0; } void mazetest() { int height = 0; int width = 0; cout << "enter height (positive integer): "; cin >> height; cout << height << "\n"; cout << "enter width (positive integer): "; cin >> width; cout << width << "\n"; mazebuilder themazebuilder(height, width); cout << "created maze builder\n"; maze themaze(themazebuilder); cout << "created maze\n"; cout << "the maze:\n" << themaze.tostring(); solvemaze(themaze); } void solvemaze(const maze& themaze) { cout << "entered solvemaze()\n"; thepath = new int[themaze.getheight()*themaze.getwidth()]; bool **washere; for(int = 0; < themaze.getheight(); i++) { for(int j = 0; < themaze.getwidth(); j++) { washere[i][j] = false; } } cout << "path initialized\n"; if(search(themaze, themaze.getentrancerow(), 0, washere, thepath, thepathlength)) { themaze.tostring(thepath, thepathlength); } } bool search(const maze& themaze, int row, int col, bool**& washere, int *apath, int currentpathlength) { if(col == themaze.getwidth()-1 && row == themaze.getexitrow()) { cout << "found exit\n"; thepathlength = currentpathlength; thepath = apath; return true; } if(washere[row][col] == true) return false; washere[row][col] = true; if(row != 0 && themaze.at(row,col).iswall(up) == false) { if(search(themaze, row-1, col, washere, apath, currentpathlength)) { apath[currentpathlength] = up; currentpathlength++; cout << "up\n"; return true; } } if(col != themaze.getwidth()-1 && themaze.at(row,col).iswall(right) == false) { if(search(themaze, row, col+1, washere, apath, currentpathlength)) { apath[currentpathlength] = right; currentpathlength++; cout << "right\n"; return true; } } if(row != themaze.getheight()-1 && themaze.at(row,col).iswall(down) == false) { if(search(themaze, row+1,col, washere, apath, currentpathlength)) { apath[currentpathlength] = down; currentpathlength++; cout << "down\n"; return true; } } if(col != 0 && themaze.at(row,col).iswall(left) == false) { if(search(themaze, row, col-1, washere, apath, currentpathlength)) { apath[currentpathlength] = left; currentpathlength++; cout << "left\n"; return true; } } cout << "dead end\n----------------------------\n"; return false; }
important methods used...
maze::at(int row, int col)
returns cell @ given row , column
maze::tostring()
or tostring(int* thepath, int thepathlength)
default: prints out maze using ascii characters cmd parameters: prints out maze solution using ascii characters cmd
cell::iswall(direction)
returns whether or not there wall in direction (directions constants declared , handled in cell)
other info: maze dynamic 2d array of cells maze constructed (can output unsolved maze properly)
the problem:
bool **washere; for(int = 0; < themaze.getheight(); i++) { for(int j = 0; < themaze.getwidth(); j++) { washere[i][j] = false;
the reason: washere
uninitialized pointer pointer bool
. dereference first pointer causing undefined behavior (such perhaps crash...).
Comments
Post a Comment