【问题标题】:Run-Time Check Failure #2 - Stack around the variable 'board' was corrupted运行时检查失败 #2 - 变量“板”周围的堆栈已损坏
【发布时间】:2021-12-31 04:50:21
【问题描述】:

我正在运行一个名为 Horse Board Game 的项目。当它快完成时,它在执行部分结束时发生了错误。错误是:

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

这里的错误在哪里?一切似乎都正常,只是出现了那个错误。

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









【问题讨论】:

  • 该错误意味着您在某处出现缓冲区溢出,从而破坏了内存。使用调试器找到它。
  • 寻求调试帮助的问题应包括minimal reproducible example,其中包含重现问题所需的所有内容,包括确切的输入。或者更好:它应该对输入进行硬编码,这样就不需要输入。这样,其他人将更容易重现该问题。目前,为了重现问题,需要其他人猜测应该输入的输入,这使得问题更难诊断。
  • 似乎一切正常,只是出现了那个错误。 -- "车子正常运行,只是刹车不灵了。"
  • 在函数enterPlayerName中,您尝试输入多个玩家名称,但在函数main中,您只能为一个最多10个字符的名称分配足够的空间。这根本不符合逻辑。您应该决定存储玩家名称的方式和位置,并相应地编写代码。
  • 我只打算输入一个代表全名的字符。例如:你的名字是 JOSH,那么输入就是 J。

标签: c++ arrays stack


【解决方案1】:

当我按照 cmets 部分中的建议将 size 设置为 4 并将 n 设置为 2 时,则该行

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

在函数updatingBoard 中导致缓冲区下溢。

第一次执行该行时,i 的值为 0j 的值为 0,这没关系,但第二次执行该行时,i 的值为0j 的值是-1。这显然是错误的,因为board[0][-1] 正在越界访问数组。

这就是您收到错误消息的原因。

您可以通过在debugger 中逐行运行您的程序来轻松看到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2013-12-13
    • 2013-12-10
    • 2015-02-09
    • 2015-05-24
    • 2014-10-20
    • 2014-07-01
    相关资源
    最近更新 更多