-
Segfault
I can't for the life of me figure out why this keeps giving me a segfault, but I'm pretty sure it has something to do with the test--;
The program is supposed to solve sudoku puzzles and this portion of my code is supposed to use a brute force approach to solve the sudoku once all single candidates have been found. Basically, it runs through each empty square from left to right starting from the top row and ending at the bottom row. If all 9 numbers cannot be legally placed in an empty square, it will backtrack to the last empty square and increment its value. If that square holds a 9, it will clear it and move back to another empty square.
Any suggestions?
Code:
if (escount > 0)
{
test = 0;
do
{
num = puzzle[row][col] + 1;
find = 0;
row = es_row[test];
col = es_col[test];
do
{
if (check_valid(puzzle, row, col, num) == 1)
{
puzzle[row][col] = num;
attempt++;
find++;
break;
}
else
{
num++;
}
}
while (num < 10);
if (find == 0)
{
test--;
back++;
row = es_row[test];
col = es_col[test];
while (puzzle[row][col] == 9)
{
puzzle[row][col] = 0;
test--;
back++;
row = es_row[test];
col = es_col[test];
}
}
else
{
test++;
}
}
while (test < escount);
}
-
Hard to say from that code-segment, but I would expect that you are getting a value out of range somewhere. Check your count up/down places, and make sure that you are not going past the size of the array anywhere [or going negative].
Segfault is caused by addressing memory that isn't owned by you, and 99.999% of the time it's caused by either using un-initialized pointer or indexes outside the range allowed for an array and other coding problems. The other 0.001% is caused by compiler bugs or such.
--
Mats
-
It may be that test = -1, if find = 0. Put in some print statements to show what is happening in your program when you run it.