tags:

views:

150

answers:

5

Been a long night, but stuck on this and now am getting "segmentation fault" in my compiler..

Basically I'm trying to display all the errors (the cout) needed. If there is more than one error, I am to display all of them.

bool validMove(const Square board[BOARD_SIZE][BOARD_SIZE], 
               int x, int y, int value)
{
    int index;
    bool moveError = true;
    const int row_conflict(0), column_conflict(1), grid_conflict(2);    
    int v_subgrid=x/3;
    int h_subgrid=y/3;

    getCoords(x,y);

    for(index=0;index<9;index++)
        if(board[x][index].number==value){
            cout<<"That value is in conflict in this row\n";
            moveError=false;
            }

    for(index=0;index<9;index++)
        if(board[index][y].number==value){
            cout<<"That value is in conflict in this column\n";
            moveError=false;    
            }



    for(int i=v_subgrid*3;i<(v_subgrid*3 +3);i++){
        for(int j=h_subgrid*3;j<(h_subgrid*3+3);j++){
            if(board[i][j].number==value){
                cout<<"That value is in conflict in this subgrid\n";            
                moveError=false;
            }           
        }
    }
return true;
}
A: 

To find out the exact line that is triggering your SEGFAULT, compile with the flag -ggdb (I'm assuming you are using GCC), and then run your program using gdb (using gdb ./name_of_the_program). When GDB starts up, use run to start the program. It will break at "main", and then execute continue. Let it run until it SEGFAULTs. Once it has segfaulted, execute backtrace (or, bt for short) to get a backtrace of the program execution. The backtrace should include the exact line that caused the SEGFAULT.

With the information that you get out of GDB, you should be able to debug your program. However, if you need more help than that, provide us with the output from backtrace so that we can be of more help.

Michael Aaron Safyan
A: 

check your indices. As you are using a fixed sized array, it might be an off-by-one error

ogni42
+1  A: 

If this is a chess board, then:

for(index=0;index<9;index++)

should be:

for(index=0;index<8;index++)

Or even better:

for(index=0;index<BOARD_SIZE;index++)

If you've got named constants, always use them in place of magic numbers.

anon
Looks like Sudoku to me.
Potatoswatter
@ Potatoswatter You may be right - still, using a named constant is the best thing to do.
anon
A: 

1) Use this function instead of directly board[x][index], etc.:

    const Square& GetSquare(
      const Square board[BOARD_SIZE][BOARD_SIZE]&,
      int x,
      int y)
    {
      assert(x >= 0);
      assert(x < BOARD_SIZE);
      assert(y >= 0);
      assert(y < BOARD_SIZE);
      return board[x][y];
    }

Test that you are on debug so that assert(false) gives an error message. Write assert(false), see the message, then delete this line. Without these assertions, I simply cannot trust your code.

2) Do not use magic numbers 9 and 3.

3) Take into account that int v_subgrid=x/3; may have a nonzero remainder, e.g., 7/3=2 and the remainder is 1. And 2/3=0. If this is what you want, ok. Just take it into account.

Daniel Daranas
A: 

I expect your seg value might be in the following section... (as well as mentioned above (using 9 instead of BOARD_SIZE) for the proir two for-loops )...

for(int i=v_subgrid*3;i<(v_subgrid*3 +3);i++){ 
    for(int j=h_subgrid*3;j<(h_subgrid*3+3);j++){ 
        if(board[i][j].number==value){ 
            cout<<"That value is in conflict in this subgrid\n";             
            moveError=false; 
        }            
    } 

I would recommend you write some robust tests for functions such as yours (unit tests). Passing in values of X or Y that are set to BOARD_SIZE - 2 or above would mean indexing out of the array size of the board.

What I'm trying to get across is, code really need to be in place to stop indexing out of bounds, hope this also helps, Neil

Neil