Unhandled exception in 0x00673CF9 in es7_24.exe: The cookie stack instrumentation code detected a stack-based buffer overrun

Hi,
I'm creating a code to simulate if the horse(chess) can make a tour of the board 8*8, the program is ready but at the end of it after it give me the response it also gives back an error:

Unhandled exception in 0x00673CF9 in es7_24.exe: The cookie stack instrumentation code detected a stack-based buffer overrun

what it is? and how can i resolve it? I'm working on visul studio.

EDIT
the error is related with the var sicurezza since i just found out that if i change it in the while from sicurezza<=1000 to sicurezza <= 10 it works. but why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

const int MAX = 8;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>

void mossa(int&, int&, int[], int[]);

int main()
{
    int scacchiera[MAX][MAX] = { 0 };
    int horizontal[MAX] = { 2, 1, -1, -2, -2, -1, 1, 2 };//x
    int vertical[MAX] = { -1, -2, -2, -1, 1, 2, 2, 1 };//y
    int currentRow = 3;
    int currentColumn = 4;
    int moveNumber;
    int contaRis = 0;
    int sicurezza = 0;
    srand(time(0));
    moveNumber = (rand() % 7);

    for (size_t i = 0; i < 64; i++)
    {
        if (scacchiera[currentRow][currentColumn] != 1)
        {
            scacchiera[currentRow][currentColumn] = 1;
            mossa(currentRow, currentColumn, horizontal, vertical);
        }
        else
            mossa(currentRow, currentColumn, horizontal, vertical);
    }
    for (size_t i = 0; i < MAX; i++)
    {
        for (size_t j = 0; j < MAX; j++)
        {
            if (scacchiera[i][j]==1)
            {
                contaRis++;
            }
        }
    }

    cout << contaRis << endl;

    return 0;
}

void mossa(int &currentRow, int &currentColumn, int horizontal[], int vertical[])
{
    int sicurezza = 0;
    int moveNumber = (rand() % 7);
    while (((currentRow + vertical[moveNumber] > 7 || currentRow + vertical[moveNumber] < 0) || (currentColumn + vertical[moveNumber] > 7 || currentColumn + vertical[moveNumber] < 0)) && sicurezza <= 1000)
    {
        sicurezza++;
        moveNumber = (rand() % 7);
    }
    currentRow += vertical[moveNumber];
    currentColumn += horizontal[moveNumber];
}
Last edited on
Check the values of your currentRow, currentColumn after mossa() returns.
I'd recommend using your debugger to step through the code, and find exactly where it crashes. Then you can look at the values of your indices at that point, to see if they're valid.
Hello MrNewbie,

Most of the time I use VS2017 and I can say that I have not seen any error message like

The cookie stack instrumentation code detected a stack-based buffer overrun

Usually what I see is is more like

The stack around variable ??? has been corrupted.

Which means that you have exceeded the boundary of an array. More often the end of the array.

As jlb says I would check the values of variables "currentRow", "currentColumn" before , during and after the while loop and then maybe after the function returns.

Andy
Replace line 31 with:
1
2
        std::cout << "currentRow=" << currentRow << " currentColumn=" << currentColumn << std::endl;
        if (scacchiera[currentRow][currentColumn] != 1)

Those indexes should be in the range [0..MAX).
moveNumber = rand() % 7;
should be
moveNumber = rand() % 8;
(and it isn't needed in main()).


More significantly,
currentColumn + vertical[moveNumber] > 7 || currentColumn + vertical[moveNumber] < 0
should be
currentColumn + horizontal[moveNumber] > 7 || currentColumn + horizontal[moveNumber] < 0



MrNewbie wrote:
EDIT
the error is related with the var sicurezza since i just found out that if i change it in the while from sicurezza<=1000 to sicurezza <= 10 it works. but why?

This is nonsense. All usage of sicurezza should be removed. It serves no purpose and could (theoretically, at least) lead to you going outside array bounds. Deal with the two errors listed above.



MrNewbie wrote:
I'm creating a code to simulate if the horse(chess) can make a tour of the board 8*8

Your code isn't going to answer that question. (Once you have made the changes listed above) it's simply going to make 64 random moves and count the number of different squares that you have landed on.

You also don't need lines 39-48. You can simply increment contaRis immediately after scacchiera[currentRow][currentColumn] = 1;
Last edited on
@OP

Your move function needs to check that the new move position is a legal chessboard position. Your mossa function doesn't do that.

You can translate back to Italian but this code shows exactly what is happening. Fixing it is easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void move(int &currentRow, int &currentColumn, int horizontal[], int vertical[])
{
    int safety = 0;
    int moveNumber = (rand() % 7);
    while (((currentRow + vertical[moveNumber] > 7 || currentRow + vertical[moveNumber] < 0) || (currentColumn + vertical[moveNumber] > 7 || currentColumn + vertical[moveNumber] < 0)) && safety <= 1000)
    {
        safety++;
        moveNumber = (rand() % 7);
    }
    currentRow += vertical[moveNumber];
    currentColumn += horizontal[moveNumber];
    
    if( currentRow < 0 or currentColumn < 0 ) //  blah blah for > 7
    {
        std::cout << "Error: " << currentRow << '*' << currentColumn << '\n';
    }
}
Topic archived. No new replies allowed.