Carson
Carson

Reputation: 1267

Does fine for other null pointers but core dumps at one - C

So I am having trouble figuring out why my code is core dumping.

Here's my code..

void findMines(Board *b, int x, int y, int row, int column)
{
    while(x != row && y != column)
    {
        if(b->boardSpaces[x][y].mineHere == '\n')
        {
            x = 0;
            y++;
            continue;
        }

        //The most hideous if statement ever.
        if(b->boardSpaces[x][y].mineHere == 'M' || b->boardSpaces[x][y].mineHere == ' ' || b->boardSpaces[x][y].mineHere == '.' || b->boardSpaces[x][y].mineHere == '\n')
        {
            switch(b->boardSpaces[x][y].mineHere)
            {
                case 'M':
                {
                    if(b->boardSpaces[x][y-1].ajacentMines == 0 || b->boardSpaces[x+1][y].ajacentMines == 0 || b->boardSpaces[x][y+1].ajacentMines == 0 || b->boardSpaces[x-1][y].ajacentMines == 0)
                    {
                        b->boardSpaces[x][y].mineHere = '.';
                    }
                    break;
                }

                case ' ':
                {
                    break;
                }
                case '.':
                {
                    break;
                }
            }
            continue;
        }else
        {
            switch(b->boardSpaces[x][y].ajacentMines)
            {
                case 0:
                {
                    if(b->boardSpaces[x][y-1].mineHere == 'M')
                    {
                        b->boardSpaces[x][y-1].mineHere = '.';
                    }
                    if(b->boardSpaces[x+1][y].mineHere == 'M')
                    {
                        b->boardSpaces[x+1][y].mineHere = '.';
                    }
                    if(b->boardSpaces[x][y+1].mineHere == 'M')
                    {
                        b->boardSpaces[x][y+1].mineHere = '.';
                    }
                    if(b->boardSpaces[x-1][y].mineHere == 'M') //core dumps here
                    {
                        b->boardSpaces[x-1][y].mineHere = '.';
                    }
                    break;
                }

                case 1:
                {
                    if(b->boardSpaces[x][y-1].mineHere == '.')
                    {
                        b->boardSpaces[x][y-1].mineHere = 'M';
                        findMines(b, x+1, y,row,column);
                    }

                    if(b->boardSpaces[x+1][y].mineHere == '.')
                    {
                        b->boardSpaces[x+1][y].mineHere = 'M';
                        findMines(b, x+1, y,row,column);
                    }

                    if(b->boardSpaces[x][y+1].mineHere == '.')
                    {
                        b->boardSpaces[x][y+1].mineHere = 'M';
                        findMines(b, x+1, y,row,column);
                    }

                    if(b->boardSpaces[x-1][y].mineHere == '.')
                    {
                        b->boardSpaces[x-1][y].mineHere = 'M';
                        findMines(b, x+1, y,row,column);
                    }
                    break;
                }
            }
            x++;
            continue;
        }
    }
}

To which board equals this...

0 1 .
1 . .
. . 1

And my variables are

x = 0
y = 0
row = 3
column = 3

What I am trying to do is check each space to the right of, left of, above, and below the indicated point to see if a 'M' is already there. (at this point 0,0 so left and above should be NULL and right and below should be 1).

At first I thought it was because my if statement was checking a NULL.

if(b->boardSpaces[x-1][y].mineHere == 'M') //core dumps here

But all the ones before it work, and x,y+1 should have also been NULL, so I'm lost.

If it matters here are my typedefs for the board and mineHere etc stuff

/*
The contents of each space in the Mines array can be either
a mine or the amount of mines adjacent to said spot.
*/
typedef union
{
    int ajacentMines; //If not a mine it adds up the amount of mines around it.
    char mineHere; //If mine then it holds char 'M'
}Mine;

/*
Constructs a board with a 2D array for the actual board holding the spots content
See union Mine above.
*/
typedef struct boards
{
    int rows, columns; //rows and columns to make the array
    Mine **boardSpaces; //a void pointer to hold said array
}Board;

Upvotes: 0

Views: 45

Answers (1)

Karoly Horvath
Karoly Horvath

Reputation: 96266

If x and y are 0, then you're checking at the location [-1][0].

That's clearly out of bounds... this is UB, consider yourself lucky that it generated an exception.

Only check places which are on the board - or alternatively, create a larger board, and don't use the edge rows and columns... that way you don't need additional checks for indices.

Upvotes: 1

Related Questions