[Solved] Crash when assigning values to array


You need to check for the validity of your array bounds and value of b.

b should obviously be:

(Pseudo code):

  MinBound <= b <= MaxBound

In your case , you can do as below:

void traps()
{
    int a,b,r;
    cout<<"Deciding placement of traps";
    for (a = 1; a <= 8; a++)
    {
        r = clock();
        cout<<"\nPress any key\n";
        getch();
        cout<<"\nPress any key\n";
        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 continue\n";
    getch();
}

12

solved Crash when assigning values to array