给数组赋值时崩溃

Crash when assigning values to array

本文关键字:崩溃 赋值 数组      更新时间:2023-10-16

这一行代码导致我的程序崩溃。原因是什么?

square [b] = 'T';

其中'square'是一个有64个元素的一维字符数组,b是一个整数。当我给出一个常数而不是b时,例如

,它不会崩溃
square [5] = 'T';

编辑:@ smileeaz引起问题的部分…

void traps()
{
    int a,b,r;
    cout<<"Deciding placement of traps";
    for (a = 1; a <= 8; a++)
    {
        r = clock();
        cout<<"nPress any keyn";
        getch();
        cout<<"nPress any keyn";
        getch();
        r = r - clock();
        b = r % 64;

        if (square [b] == '.')
        {
            square[b] = 'T';
        }
    }
    cout<<"nTraps generated..... Press any key to continuen";
    getch();
}

这是完整的代码…

#include <iostream>
#include <conio.h>
#include <ctime>              //mainly to generate random numbers
#include <cstdlib>               // for clear screen
using namespace std;
int trap_hit = 0;              //0 => not stepped on trap
                               //1 => stepped on trap
char m;                //where the player wants to move
int player_position = 0;
//The game board "." denotes empty square
char square [64] {'O','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','X'};
//place traps
void traps()
{
    int a,b,r;
    cout<<"Deciding placement of traps";
    for (a = 1; a <= 8; a++)
    {
        r = clock();
        cout<<"nPress any keyn";
        getch();
        cout<<"nPress any keyn";
        getch();
        r = r - clock();
        b = r % 64;
        if (square [b] == '.')
        {
            square[b] = 'T';
            cout<<"Debug1";    //for debugging
        }
    }
    cout<<"nTraps generated..... Press any key to continuen";
    getch();
}
//to draw the board
void board()
{
    system ("cls");
    cout<<"           Dungeon crawln";
    cout<<"INSTRUCTIONS:use the keys "w", "s", "a" and "d"n to move up, down, left and right respectively.n Do not touch the Traps (T) or enemies (E)n";
    cout<<"n   "<<square [0]<<"   "<<square [1]<<"   "<<square [2]<<"   "<<square [3]<<"   "<<square [4]<<"   "<<square [5]<<"   "<<square [6]<<"   "<<square [7]<<"n";
    cout<<"n   "<<square [8]<<"   "<<square [9]<<"   "<<square [10]<<"   "<<square [11]<<"   "<<square [12]<<"   "<<square [13]<<"   "<<square [14]<<"   "<<square [15]<<"n";
    cout<<"n   "<<square [16]<<"   "<<square [17]<<"   "<<square [18]<<"   "<<square [19]<<"   "<<square [20]<<"   "<<square [21]<<"   "<<square [22]<<"   "<<square [23]<<"n";
    cout<<"n   "<<square [24]<<"   "<<square [25]<<"   "<<square [26]<<"   "<<square [27]<<"   "<<square [28]<<"   "<<square [29]<<"   "<<square [30]<<"   "<<square [31]<<"n";
    cout<<"n   "<<square [32]<<"   "<<square [33]<<"   "<<square [34]<<"   "<<square [35]<<"   "<<square [36]<<"   "<<square [37]<<"   "<<square [38]<<"   "<<square [39]<<"n";
    cout<<"n   "<<square [40]<<"   "<<square [41]<<"   "<<square [42]<<"   "<<square [43]<<"   "<<square [44]<<"   "<<square [45]<<"   "<<square [46]<<"   "<<square [47]<<"n";
    cout<<"n   "<<square [48]<<"   "<<square [49]<<"   "<<square [50]<<"   "<<square [51]<<"   "<<square [52]<<"   "<<square [53]<<"   "<<square [54]<<"   "<<square [55]<<"n";
    cout<<"n   "<<square [56]<<"   "<<square [57]<<"   "<<square [58]<<"   "<<square [59]<<"   "<<square [60]<<"   "<<square [61]<<"   "<<square [62]<<"   "<<square [63]<<"n";
}
//allow user to move
void pl_move()
{
    char move_dir;
    invalid_move:
    cout<<"nPress w, a, s or d ...... ";
    cin>>move_dir;
    cin.clear();
    cin.ignore(100000, 'n');
    //allow the player to move
    if (move_dir == 'w')
    {
        if (player_position < 8)               //trying to move out of the board
        {
            cout<<"Invalid move";
            goto invalid_move;
        }
        square [player_position] = '.';
        player_position = (player_position - 8);
        if (square [player_position] == 'T')
        {
            trap_hit = 1;
        }
        square [player_position] = 'O';
    }
    else if (move_dir == 's')
    {
        if (player_position >55 && player_position <64)                 //trying to move out of the board
        {
            cout<<"Invalid move";
            goto invalid_move;
        }
        square [player_position] = '.';
        player_position = (player_position + 8);
        if (square [player_position] == 'T')
        {
            trap_hit = 1;
        }
        square [player_position] = 'O';
    }
    else if (move_dir == 'a')
    {
        if ((player_position % 8) == 0)                     //trying to move out of the board
        {
            cout<<"Invalid move";
            goto invalid_move;
        }
        square [player_position] = '.';
        player_position = (player_position - 1);
        if (square [player_position] == 'T')
        {
            trap_hit = 1;
        }
        square [player_position] = 'O';
    }
    else if (move_dir == 'd')
    {
        if ((player_position % 8) == 7)                         //trying to move out of the board
        {
            cout<<"Invalid move";
            goto invalid_move;
        }
        square [player_position] = '.';
        player_position = (player_position + 1);
        if (square [player_position] == 'T')
        {
            trap_hit = 1;
        }
        square [player_position] = 'O';
    }
    else
    {
        cout<<"nInvalid move";
        goto invalid_move;
    }
}

int main()
{
    traps();       
    while(trap_hit == 0 && player_position != 63)
    {
        board();
        pl_move();
    }
    board();
    if (trap_hit == 1)
    {
        cout<<"nYou have stepped on a trap. You lose!";
    }
    else if (player_position == 63)
    {
        cout<<"nYou have retrieved the treasure and won!";
    }
    getch();
    return 0;
}

您需要检查您的数组边界和b值的有效性。

b显然应该是:

(伪代码):

  MinBound <= b <= MaxBound

在你的情况下,你可以这样做:

void traps()
{
    int a,b,r;
    cout<<"Deciding placement of traps";
    for (a = 1; a <= 8; a++)
    {
        r = clock();
        cout<<"nPress any keyn";
        getch();
        cout<<"nPress any keyn";
        getch();
        cout<<"f1";  //debug
        r = clock() - r; //Edited to fix negetive values
        cout<<"f2";
        b = r % 64;
        cout<<"f3";
        //if (square [b] == '.')
        //{
        if(b < 0 || b>=64) //Validation Here
        continue;
            square[b] = 'T';
            cout<<"Debug1";
        //}
    }
    cout<<"nTraps generated..... Press any key to continuen";
    getch();
}