简体   繁体   中英

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

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;
}```









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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM