c++ - Run-Time Check Failure #2 Stack around the variable 'maze' were corrupted -
run-time check failure #2 stack around variable 'maze' corrupted. whenever compile , run program, receive error whenever program finishes running. believe problem happening in addpaths function in implementation. posted code in case. program creating maze , addpaths function "digging" paths user move through. path direction chosen @ random , paths drawn @ number spaces on maze.
header:
const int height = 3; const int width = 5; class coordinate { public: int row, column; }; class maze { public: maze(); ~maze(); void buildmaze(); void displaymaze(); void addpaths(); void startgame(); void moveplayer(int); bool solved(); int getkey(); void adddestinationtogrid(); private: char grid[width][height]; coordinate player; const static char player = 'p'; const static char destination = 'x'; const static char start = 's'; const static char path = ' '; const static char wall = (char)219; };
implementation:
#include <iostream> #include "maze.h" #include <windows.h> #include <stack> using std::cout; using std::endl; maze::maze() { buildmaze(); } maze::~maze() { cout << "yay"; } void maze::buildmaze() { (int x = 0; x <= height-1; x++) { (int y = 0; y <= width-1; y++) { grid[x][y] = wall; //setconsoletextattribute(getstdhandle(std_output_handle), 4); } } } void maze::displaymaze() { (int x = 0; x <= height-1; x++) { (int y = 0; y <= width-1; y++) { cout << grid[x][y]; } cout << endl; } } void maze::startgame() { int input; { input = getkey(); moveplayer(input); } while (!solved()); } bool maze::solved() { return true; } void maze::moveplayer(int direction) { if (direction == vk_up || direction == vk_down || direction == vk_left || direction == vk_right) { coord newcoord = { player.column + 1, player.row + 1 }; setconsolecursorposition(getstdhandle(std_output_handle), newcoord); if (grid[player.row][player.column] == start) { cout << start; } else { cout << path; } } } int maze::getkey() { int result = 0; while (!solved() && result == 0) { short max_short = 0x7fff; //111111111111111 if (getasynckeystate(vk_left) & max_short) { result = vk_left; } else if (getasynckeystate(vk_up) & max_short) { result = vk_up; } else if (getasynckeystate(vk_right) & max_short) { result = vk_right; } else if (getasynckeystate(vk_down) & max_short) { result = vk_down; } } return result; } void maze::addpaths() { coordinate currentlocation; coordinate startlocation; //coordinate endlocation; //not used yet std::stack<coordinate> mystack; currentlocation.row = (((rand() % height) / 2) * 2); currentlocation.column = (((rand() % width) / 2) * 2); startlocation = currentlocation; grid[currentlocation.row][currentlocation.column] = start; player = currentlocation; { bool canmoveup = !(currentlocation.row == 0 || grid[currentlocation.row - 2][currentlocation.column] != wall); bool canmovedown = !(currentlocation.row == height - 1 || grid[currentlocation.row + 2][currentlocation.column] != wall); bool canmoveleft = !(currentlocation.column == 0 || grid[currentlocation.row][currentlocation.column - 2] != wall); bool canmoveright = !(currentlocation.column == width - 1 || grid[currentlocation.row][currentlocation.column + 2] != wall); if (canmoveup || canmovedown || canmoveleft || canmoveright) { mystack.push(currentlocation); //choose random location dig bool movefound = false; while (movefound != true) { int direction = rand() % 4; if (direction == 0 && canmoveup) { movefound = true; grid[currentlocation.row - 2][currentlocation.column] = path; grid[currentlocation.row - 1][currentlocation.column] = path; currentlocation.row -= 2; } else if (direction == 1 && canmovedown) { movefound = true; grid[currentlocation.row + 2][currentlocation.column] = path; grid[currentlocation.row + 1][currentlocation.column] = path; currentlocation.row += 2; } else if (direction == 2 && canmoveleft) { movefound = true; grid[currentlocation.row][currentlocation.column - 2] = path; grid[currentlocation.row][currentlocation.column - 1] = path; currentlocation.column -= 2; } else if (direction == 3 && canmoveright) { movefound = true; grid[currentlocation.row][currentlocation.column + 2] = path; grid[currentlocation.row][currentlocation.column - 2] = path; currentlocation.column += 2; } } } else if (!mystack.empty()) { currentlocation = mystack.top(); mystack.pop(); } } while (!mystack.empty()); adddestinationtogrid(); } void maze::adddestinationtogrid() { int randomrow = rand() % height; int randomcolumn = rand() % width; while (grid[randomrow][randomcolumn] != path) { randomrow = rand() % height; randomcolumn = rand() % width; } grid[randomrow][randomcolumn] = destination; }
main:
#include <iomanip> #include <iostream> #include "maze.h" using namespace std; int main() { maze maze; maze.addpaths(); maze.displaymaze(); maze.startgame(); /*if (maze.solved()) { cout << " win!"; }*/ }
there 2 problems. 1 not consistent in order access elements of grid
. declared grid[width][height]
(but not all) accesses use height
based index first. isn't causing problems since width
greater height
, stay within object's memory when doing normal accesses it.
the problem line:
grid[currentlocation.row][currentlocation.column - 2] = path;
in moveright handler. column offset should + 1
, not - 2
. way can cause write memory before first element of grid
.
Comments
Post a Comment