Run-Time Check Failure #2 - Stack around the variable 'board' was corrupted is the error I'm getting.

The program generates it's own maze and then will solve it.

The error seems to happen after the program is done with EVERYTHING.

I'm using Microsoft Visual Studio 2008 beta.

#include <iostream>
#include <string>
#include <ctime>

using namespace std;

const int SIZE = 11; // must be odd number greater then 1 

void initBoard(int board[SIZE][SIZE]);
void displayBoard(int board[][SIZE]);
void generateBoard(int board[][SIZE]);
void changeBoard(int board[][SIZE],int changeNumber,int r,int c);
void pause();
void dummyFunction();
bool isComplete(int board[][SIZE]);
bool isLegalSpot(int board[][SIZE],int r,int c);
bool search(int board[][SIZE],int r,int c);


int main(){

	int board[SIZE][SIZE];

	initBoard(board);
	displayBoard(board);
	generateBoard(board);
	displayBoard(board);
	dummyFunction();

	return 0;
}
void initBoard(int board[SIZE][SIZE]){
	int row_alt = 1;
	int col_alt = 1;
	for(int r=0;r<SIZE;r++){
		for(int c=0;c<SIZE;c++){
			if(row_alt==1&&col_alt==1){
				board[r][c]=1;
			}
			else{
				board[r][c]=0;
			}
			col_alt*=-1;
		}
		row_alt*=-1;
		col_alt*=-1;
	}
}
void displayBoard(int board[][SIZE]){
	for(int r=0;r<SIZE;r++){
		for(int c=0;c<SIZE;c++)
			cout << board[r][c] << " ";
		cout << endl;
	}
	cout << "\n\n\n";
}
void generateBoard(int board[][SIZE]){

	int nextNumber = 1;
	while(!isComplete(board)){
		int rand_r = rand()%(SIZE);
		int rand_c = rand()%(SIZE);
		if(isLegalSpot(board,rand_r,rand_c)){
			board[rand_r][rand_c] = ++nextNumber;
			changeBoard(board,nextNumber,rand_r,rand_c);
			//displayBoard(board);
			//pause();
		}
	}
}
void changeBoard(int board[][SIZE],int changeNumber,int r,int c){
	if(r-1>=0&&board[r-1][c]!=0&&board[r-1][c]!=changeNumber){board[r-1][c]=changeNumber;  changeBoard(board,changeNumber,r-1,c);} // West
	if(c-1>=0&&board[r][c-1]!=0&&board[r][c-1]!=changeNumber){board[r][c-1]=changeNumber; changeBoard(board,changeNumber,r,c-1);} //North
	if(r+1<=SIZE&&board[r+1][c]!=0&&board[r+1][c]!=changeNumber){board[r+1][c]=changeNumber; changeBoard(board,changeNumber,r+1,c);}//East
	if(c-1<=SIZE&&board[r][c+1]!=0&&board[r][c+1]!=changeNumber){board[r][c+1]=changeNumber; changeBoard(board,changeNumber,r,c+1);} //South
}
bool isComplete(int board[][SIZE]){
	return (board[0][0]!=1&&board[SIZE-1][SIZE-1]!=1)&&(board[0][0]==board[SIZE-1][SIZE-1]);
}
bool isLegalSpot(int board[][SIZE],int r,int c){
	if(((r==1&&c==1)||(r%2!=0&&c%2!=0))||board[r][c]!=0)return false;

	if(r-1>0&&board[r-1][c]!=0) return true;
	else if(r+1<SIZE&&board[r+1][c]!=0)return true;
	else if(c-1>0&&board[r][c-1]!=0)return true;
	else if(c+1<SIZE&&board[r][c+1]!=0)return true;
	else return false;
}
bool search(int board[][SIZE],int r,int c){
	return true;
}
void pause(){
	cout << "Enter a key to continue...";
	string dummy;
	cin >> dummy;
}
void dummyFunction(){
	for(int x=0;x<10;x++)
		cout << x << ") " << " dummy function call\n";
}

I haven't examined you algorithm very deeply, but somewhere in changeBoard() you are accessing an element outside of the board array. The error is recorded on the stack (the board is also stored on the stack), but only noticed when you try to return from main().

changeBoard(board,nextNumber,rand_r,rand_c);

rand_r can be SIZE-1 in the function you are accessing rand_r+1 and hence the corruption.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.