-2

I am writing a Sudoku solver to practice my skills for next semester. When I try to run it I get a segmentation fault. Using gdb, I track it to this code function:

vector<int> sudoku::valid_set(int row, int col)
{
    vector<int> valids;
    valids.push_back(0);

    int rows[9] = {0},
        cols[9] = {0},
        Grid[9] = {0};

    for (int i = 0; i < 9; i++)
    {
        if (i != col) 
        // we don't want to test the input cell because this 
        // will cause an incorect return
            rows[grid[row][i] - 1]++;
    }
    for (int i = 0; i < 9; i++)
    {
        // make sure current cell is not 0
        if (i != row) //we dont' want to test the input cell
            cols[grid[i][col] - 1]++;
    }
    // do the same steps for the mini grid using integer division 
    for (int i = row / 3 * 3; i < row / 3 * 4; i++)
    {
        for (int j = col / 3 * 3; i < col / 3 * 4; i++)
        {
            if (i != row && j != col)
                Grid[grid[i][j] - 1]++;
        }
    }
    // using the three arrays, find out what 
    // values need to go into the valids vector.  
    for (int i = 0; i < 9; i++)
    {
        if (rows[i] == 0 && cols[i] == 0 && Grid[i] == 0)
        {
            int val = i + 1;
            valids.push_back(val);
        }
    }
    return valids;
}

More specifically I think the error is occurring at the line valids.push_back(val) but I cannot for the life of me seem to figure out why. Maybe I am missing something blatantly obvious but I just do not know. Can anybody offer some guidance?

chrisb2244
  • 2,940
  • 22
  • 44
gsoble
  • 31
  • 1
  • 2
  • 9
  • 2
    In your initial loops, `grid[row][i] == 0`. So your index into `rows` becomes `rows[0 - 1]`, which is outside the bounds of an array. – Thomas Matthews Nov 26 '14 at 02:08
  • What is `grid`? Noting the difference to `Grid`, as highlighted in the line `Grid[grid[i][j] -1]++;`. I assume some global static object? – chrisb2244 Nov 26 '14 at 02:11
  • Oh because a cell that is not filled in will have a value of 0, so I am subtracting 1 from it! – gsoble Nov 26 '14 at 02:12
  • Sorry, this is in a class called sudoku and grid is a class member. It has certain indices with values 1-9 that are read from a file. Anything that is not filled in (IE what needs to be solved) is set to 0. – gsoble Nov 26 '14 at 02:13

1 Answers1

1

Looks like you could benefit from some extra code that performs boundary checks on your arrays:

   for (int i = 0; i < 9; i++)
    {
        if (i != col)
        {
           if (row >= MAXIMIM_ROWS)
               throw An_Error();
           int rows_index = grid[row][i];
           if (rows_index <= 0)
               throw Convulsions();

        // we don't want to test the input cell because this 
        // will cause an incorect return
            rows[grid[row][i] - 1]++;
    }

You should put in your own error handling, especially because parameters to the function could contain any value, especially indices outside the range of your arrays.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154