Buffer Overrun Error

Dec 5, 2008 at 11:42pm
Hello,

I have encountered a very annoying error in my "game". Before I explain what my error is, let me tell you what my program is accomplishing. There is a 5x5 grid made up of '.'s stored in a "char Grid[5][5];" array. A Unit, "char Unit = 'O';" moved around this grid with the "W, S, A, and D" keys with the use of "getch();" from "char dirInput;".

There is the error:

1
2
3
4
5
A buffer overrun has occurred in Movement.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.

For more details please see Help topic 'How to debug Buffer Overrun Issues'.

Break | Continue | Ignore 


Ignore is grayed out, and no 'How to debug Buffer Overrun Issues' topic exists, I have searched the help database, searched MSDN, and done a google search, but nothing.

My program builds with no problem, but the error appears instantly when the program begins.

Here is my code:

[code=cpp]#include <iostream>
#include <conio.h>
using namespace std;

int main() { //main function
//Variables
int posX=2; int posY=2; //coordnates of UNIT
char Unit = 'O'; //the UNIT
char dirInput; //input of DIRECTION for UNIT
char Grid[5][5]; //grid for UNIT to move on
//init Grid --------------------
for(int a=0;a<25;a++) {
for(int b=0;b<25;b++) {
Grid[a][b] = '.'; // sets all values in "Grid[5][5]" to '.'
}
}
//------------------------------

//Code
while(true) { //main loop

cout << posX << ", " << posY<<endl<<endl; //show unit coordinates
Grid[posX][posY]=Unit; //set unit position

//print board - starting pos = (2,2) ------------------------------------------------
cout << Grid[0][4] << Grid[1][4] << Grid[2][4] << Grid[3][4] << Grid[4][4] << endl
<< Grid[0][3] << Grid[1][3] << Grid[2][3] << Grid[3][3] << Grid[4][3] << endl
<< Grid[0][2] << Grid[1][2] << Grid[2][2] << Grid[3][2] << Grid[4][2] << endl
<< Grid[0][1] << Grid[1][1] << Grid[2][1] << Grid[3][1] << Grid[4][1] << endl
<< Grid[0][0] << Grid[1][0] << Grid[2][0] << Grid[3][0] << Grid[4][0] << endl;
//-----------------------------------------------------------------------------------

//move unit -----------------------
dirInput = getch();
switch(dirInput) {
case 'w':
case 'W':
//move up
Grid[posX][posY] = '.';
posY++;
break;
case 'a':
case 'A':
//move left
Grid[posX][posY] = '.';
posX--;
break;
case 's':
case 'S':
//move down
Grid[posX][posY] = '.';
posY--;
break;
case 'd':
case 'D':
//move right
Grid[posX][posY] = '.';
posX++;
break;
case 'q':
case 'Q':
//quit
return 0;
break;
default: ;
}
//---------------------------------
system("CLS"); //clear the screen
}
return 0;
}[/code]

I hope I have provided as much information about the code with the comments / description. Please help me fix this error, it's driving me nuts!

Thanks in advance,
CheesyBeefy
Last edited on Dec 5, 2008 at 11:46pm
Dec 6, 2008 at 12:12am
1
2
3
4
5
for(int a=0;a<25;a++) {		  
		for(int b=0;b<25;b++) {	  
			Grid[a][b] = '.';	  // sets all values in "Grid[5][5]" to '.'
		}						   
	}	

That will get to Grid[24][24] which is WAY outside the bounds of your array...you want a<5 and b<5.
Dec 6, 2008 at 1:10am
Well now my program runs, but the grid appears very strangely, with weird characters.
Here is what I mean: http://img214.imageshack.us/my.php?image=strangegridmw3.png

Any reason for this?

EDIT:

Nevermind, I accidentally set it to 4, not 5.

Thanks for the help, it works perfectly now! :)
Last edited on Dec 6, 2008 at 1:51am
Topic archived. No new replies allowed.