C++ buffer overrun

14.3k Views Asked by At

I'm learning C++ and whilst I have a reasonable understanding of C# I've never run into this issue before. With a simple program that places chess pieces on an imaginary board (an enumerated array) and then assigns the squares which would have pieces at the start their pieces, you are then asked for coordinates and the program returns what is on that square. It displays the correct piece yet will always crash in non-debugging mode and display a buffer overrun in Visual Studio debugging. It's quite short so I'll show all the code.

#include <iostream>
#include <string>
using namespace std;
int main() {
enum Chessboard {
    Blank,
    Pawn,
    Rook,
    Knight,
    Bishop,
    King,
    Queen
};
Chessboard board[8][8] = { Blank };
for (int x = 1; x < 8; x++)
{
    board[1][x] = Pawn;
    board[8][x] = Pawn;
}
board[7][0] = Rook;
board[7][1] = Knight;
board[7][2] = Bishop;
board[7][3] = King;
board[7][4] = Queen;
board[7][5] = Bishop;
board[7][6] = Knight;
board[7][7] = Rook;
board[0][0] = Rook;
board[0][1] = Knight;
board[0][2] = Bishop;
board[0][4] = King;
board[0][3] = Queen;
board[0][5] = Bishop;
board[0][6] = Knight;
board[0][7] = Rook;

int X = 0;
int Y = 0;
bool Error = false;
cout << "Enter the coordinates of a square on a chessboard to see what is on there at    the start of the game (1 number at a time)" << endl;
do {
    cin >> X;
    X--;
    Error = false;
    if (X < 0 || X > 7)
    {
        cout << "That's not on the board" << endl;
        Error = true;
    }
} while (Error = false);
do {
    cin >> Y;
    Y--;
    Error = false;
    if (Y < 0 || Y > 7)
    {
        cout << "That's not on the board" << endl;
        Error = true;
    }
} while (Error = false);

string Name = "";
Chessboard Piece = board[X][Y];
switch (Piece)
{
case Blank: Name = "nothing";
    break;
case Pawn: Name = "a Pawn";
    break;
case Rook: Name = "a Rook";
    break;
case Knight: Name = "a Knight";
    break;
case Bishop: Name = "a Bishop";
    break;
case King: Name = "a King";
    break;
case Queen: Name = "a Queen";
    break;
default: Name = "Somehow you missed the board";
    break;
} 

cout << "On " << ++X << "," << ++Y << " there is " << Name << endl;

return 0;
}
5

There are 5 best solutions below

1
On BEST ANSWER

You'll certainly get an overrun here:

Chessboard board[8][8] = { Blank };
for (int x = 1; x < 8; x++)
{
  board[1][x] = Pawn;
  board[8][x] = Pawn; 
}

There's no board[8][]. You have board[0][] through board[7][] available.

0
On

As stated by everybody else, its the board[8][x] = Pawn that is causing the error.

Though this appears to be a test program rather than something that will go into production but still one word of caution that I would like to advise, always try to avoid numbers / hard coded strings/integers or anything else, in your code for reason that you usually end up doing things like this. And one day when project goes into production and may be your boss will decide to change the value to say, 100 x 100, you will have a very hard time doing things.

Good Ways to do this :

static const int BoardSize = 10;

or

#define BoardSize 10;
0
On
for (int x = 0; x < 8; x++)
{
    board[0][x] = Pawn;
    board[7][x] = Pawn;
}

I think 7 is the maximum for this array.

0
On

In C, C++ and C# indices of arrays start from 0 to size of the array - 1. So for example this loop

for (int x = 1; x < 8; x++)
{
    board[1][x] = Pawn;
    board[8][x] = Pawn;
}

has to be rewritten as

for ( int x = 0; x < 8; x++)
{
    board[1][x] = Pawn;
    board[6][x] = Pawn;
}

provided that the array is defined as

Chessboard board[8][8] = { Blank };

Also it would be a good idea to introduce a mnemonic name for magic number 8 and use this name everwhere instead of the number.

3
On

You are going outside the boundaries of your matrix

   board[1][x] = Pawn;
   board[8][x] = Pawn;

You declared it 8x8 so index 0..7 are to be used.