Hi All,

I am writing an MFC Tic Tac Toe app and I am getting a strange error when debugging it. This module loads a bunch of BOOL variables to keep track of whether or not the tile next to it matches (i.e is the tile next to me also an 'X' tile or is it an 'O' tile). The problem I'm having is that the if() statement checks to make sure that the tile that I am currently checking is not out of bounds (off the board). Here is an example:

if (k - 1 < 0 || j - 1 < 0);
else
{
	if (tileMap[k][j]->GetTileType() == tileMap[k - 1][j - 1]->GetTileType() &&
		tileMap[k][j]->GetTileType() != -1)
		tileMap[k][j]->ulCorner = TRUE;
}

Just to let you know tileMap is of type

CTile *tileMap[3][3]

(CTile is inherited from CBitmapButton, it is a 2D array of pointers to store the tiles) Also, the if statement just ends in a semicolon because if that is true I just want the else block skipped completely and to move onto the next segment. So if k - 1 or j - 1 < 0 this means that it is off the board and the else statement should not execute because it would be an array out of bounds exception. So if the if() statement is true the else block should not excecute at all because k-1 or j-1 would be out of the array. When running the debugger and I get to this segment, as soon as it gets past the if() statement I get the error "Access violation reading location 0xcccccd40." because it is still trying to call GetTileType() on an array index that is outside the array bounds. If I comment out the second if statement the code continues past this point without error. How can I make it so that this error doesn't keep haunting my life?? :confused:

Look closely at your if statement

if (k - 1 < 0 || j - 1 < 0);

You have a semi-colon where it shouldn't be.

gerard4143 is mistaken -- the semicolon is correct in this context.

However, I can see three potential problems and I can't tell from the code you've posted whether they're relevant:

1) What types do j and k have? If they are unsigned, then j-1 and k-1 will never be < 0.

2) Is it possible that j and k may have values that are too large? You don't seem to be checking for that.

3) Are you sure that every element of tileMap has an appropriate value?

gerard4143 is mistaken -- the semicolon is correct in this context.

Ooops, your right my mistake.

gerard4143 is mistaken -- the semicolon is correct in this context.

However, I can see three potential problems and I can't tell from the code you've posted whether they're relevant:

1) What types do j and k have? If they are unsigned, then j-1 and k-1 will never be < 0.

2) Is it possible that j and k may have values that are too large? You don't seem to be checking for that.

3) Are you sure that every element of tileMap has an appropriate value?

1) j and k are of type int (signed)

2) I do check for values that are too large when for example I am checking the tiles to the right of the current one (ex. if (j + 1 > 2); <<-- this checks for array out of bounds exception that extends past the end of the array)

3) Every element in the array is properly initialized but the reason I check with the if/else statements is to make sure that when I am checking the tiles around the current tile that it does not exceed the bounds of the array.

The problem is that even when if(j - 1 < 0) is TRUE the code in the else block causes the error. I know that it would cause an error to run that code because the if() statement that checks to make sure I don't exceed the bounds of the array is true. The else block should not even be considered if the if() statement is true.

Bump

commented: Never BUMP your threads. We'll get to them when we getr to them. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.