0

I am running project called Horse Board Game. When it got almost done, it happened an error at the end of the execution part. The error is:

Run-Time Check Failure #2 - Stack around the variable 'board' was corrupted.

Where is the mistake here? Everything seems to work properly, only that error occurs.

#include <iostream>
#include <ctime>
#include <Windows.h>

using namespace std;

void printHorizontalLine(int size) {
    for (int i = 0; i < size; i++) {
        cout << " -----------";
    }
    cout << "\n";
}
void printVerticalLine(int size) {
    cout << "|";
    for (int i = 0; i < size; i++) {
        cout << "           |";
    }
    cout << "\n";
}
void displayBoard(int size, char board[50][50]) {
    for (int i = 0; i < size; i++) {
        printHorizontalLine(size);
        printVerticalLine(size);
        cout << "|";
        if (i % 2 == 0) {
            for (int j = 0; j < size; j++) {
                cout << "    " << board[i][j] << "      |";
            }
        }
        else {
            for (int j = size - 1; j >= 0; j--) {
                cout << "    " << board[i][j] << "      |";
            }
        }
        cout << "\n";
        printVerticalLine(size);
    }
    printHorizontalLine(size);
}
void init_board(char board[50][50], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            board[i][j] = ' ';
        }
    }
}
void enteringPlayerName(char name[], int n) {
    for (int i = 0; i < n; i++) {
        cout << "Player " << i + 1 << " enter name: " << "\n";
        cin >> name[i];
    }
}
void beginningPos(int n, int pos[]) {
    for (int i = 0; i < n; i++) {
        pos[i] = -1;
    }
}
void rollDice(int max, char board[50][50], int pos[], char name[], int size, int n, int cur) {
    int step = rand() % max + 1;
    cout << name[cur] << " roll " << step << "\n";
    if (step + pos[cur] > size * size - 1) {
        cout << "Cannot move forward\n";
    }
    else {
        pos[cur] += step;
        for (int i = 0; i < n; i++) {
            if ((pos[cur] == pos[i]) && (i != cur)) {
                pos[i] = -1;
                cout << name[i] << " is kicked to the Start!!\n";
            }
        }
    }
}
void updatingBoard(char board[50][50], char name[], int pos[], int n, int size) {
    init_board(board, size);
    for (int k = 0; k < n; k++)
    {
        int x, i, j;
        x = pos[k];
        i = x / size;
        j = x % size;
        board[i][j] = name[k];
    }
}
char playGame(int max, int n, int size, char board[50][50], int pos[], char name[], int finish_point) {
    char winner = ' ';
    int cur_turn = 0;
    
    while (winner == ' ') {
        rollDice(max, board, pos, name, size, n, cur_turn % n);
        updatingBoard(board, name, pos, n, size);
        displayBoard(size, board);
        if (pos[cur_turn % n] == finish_point) winner = name[cur_turn % n];
        cur_turn++;
    }
    return winner;
}
int main() {
    int size, n;
    cin >> size;
    cin >> n;
    const int MAX = 50;
    int max = 3;
    int pos[200];
    char name[10];
    char winner = ' ';
    char board[MAX][MAX];
    int finish_point = size * size - 1;
    srand(time(0));
    enteringPlayerName(name, n);
    init_board(board, size);
    beginningPos(n, pos);
    winner = playGame(max, n, size, board, pos, name, finish_point);
    if (winner == ' ') cout << "Tie game";
    else cout << winner << " is the winner";
    return 0;
}```









Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 2
    The error means you have a buffer overflow somewhere, corrupting memory. Use a debugger to find it. – Remy Lebeau Nov 21 '21 at 02:27
  • 1
    Questions seeking debugging help should include a [mre], which consists of everything required to reproduce the problem, including the exact input. Or better: It should hard-code the input, so that no input is required. That way, it will be easier for other people to reproduce the problem. Currently, it is necessary for other people to guess the input that should be entered in order to reproduce the problem, which makes the problem harder to diagnose. – Andreas Wenzel Nov 21 '21 at 02:32
  • 1
    *Everything seems to work properly, only that error occurs.* -- "The car runs properly, except that the brakes don't work." – PaulMcKenzie Nov 21 '21 at 02:51
  • In the function `enterPlayerName`, you attempt to input several player names, but in the function `main`, you only allocate enough space for one name of up to 10 characters. This does not make sense. You should decide how and where you want to store the player names, and write the code accordingly. – Andreas Wenzel Nov 21 '21 at 03:01
  • I only intend to input a character which stands for a whole name. For example: your name is JOSH then the input will be J. – Thanh Phương Nov 21 '21 at 03:07
  • @ThanhPhương: Ah, ok, then that part of your code looks correct, as long as `n` is not more than `10`. If `n` is more than 10, then it will overflow the buffer `name`. – Andreas Wenzel Nov 21 '21 at 03:10
  • @RemyLebeau: Actually, the problem seems to be an underflow, not an overflow. At least that it what AddressSanitizer is reporting when I run the code. – Andreas Wenzel Nov 21 '21 at 03:12
  • @AndreasWenzel so how can I make this out? – Thanh Phương Nov 21 '21 at 03:18
  • What is `size` and `n` supposed to be set to? These numbers are set by the user, but you did not state what the user is supposed to input. – Andreas Wenzel Nov 21 '21 at 03:22
  • ```size``` is size of the board and ```n``` is the the number of the players @AndreasWenzel – Thanh Phương Nov 21 '21 at 03:27
  • Yes, and what are they supposed to be set to, in order to reproduce the error? I am asking because if I set these values to unintended values, then this may cause other errors in your program, and then I will be hunting the wrong errors. As already pointed out, if you set `n` to a value higher than 10, it will cause an additional bug (buffer overflow). That is why I must know what values I am supposed to set these variables to. – Andreas Wenzel Nov 21 '21 at 03:31
  • You can set ```size``` is 4 and ```n``` is 2 @AndreasWenzel – Thanh Phương Nov 21 '21 at 03:37

1 Answers1

0

When I set size to 4 and n to 2 as recommended in the comments section, then the line

board[i][j] = name[k];

in the function updatingBoard causes a buffer underflow.

The first time that line is executed, i has the value 0 and j has the value 0, which is ok, but the second time that line is executed, i has the value 0 but j has the value -1. This is obviously wrong, because board[0][-1] is accessing the array out of bounds.

That is the reason for the error message that you are receiving.

You can easily see this by running your program line by line in a debugger.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39