-2

I have to make an n x n sudoku solver. The user inputs the n, and then the output is a solved sudoku puzzle. I did the majority of it, but I keep getting a segmentation fault. Any help would be greatly appreciated. I think it has to do with my recursive function.

int findValues(int** sudoku, int size, int a[], int r, int c)
{
    int n=0;
    int i,j;
    int s=(int)(sqrt(size));
    int b[size+1];
    memset(b,0,size*size*sizeof(int));

    //checks numbers in row
    for(i=0; i<size; i++)
        b[sudoku[r][i]]=1;

    //checks number in column
    for(i=0; i<size; i++)
        b[sudoku[i][c]]=1;

    //checks number in block
    r=(r/s)*s;
    c=(c/s)*s;
    for(i=r; i<r+s; i++)
        for(j=c; j<c+s;j++)
            b[sudoku[i][j]]=1;

    //Fill array a[] with no.s disappeared in current row, column and block
    for(i=1;i<=size; i++)
        if(!b[i])
            a[n++]=i;

    return n;
}

//Function to solve the sudoku board
void Solve(int size, bool &stop)
{
    int** sudoku;
    sudoku=new int*[size];
    for(int z=0; z<size; z++)
        sudoku[z] = new int[size];

    int i,j;
    int a[size+1];
    memset(a,0,size*size*sizeof(int));
    int n=0;

    if(stop) //true if user does not want more solutions
        return;

    if(isFull(sudoku, size))    //true if now sudoku board is solved completely
    {
        //shows the solution        
        displaySolution(sudoku, size);
        char more;
        cout << "Press 0 for 'END'\n";
        cin >> more;

        if(more != '1')
            stop = true;
        return;//so that only one solution will appear, and it does not       continuously repeat       
    }

    //to find empty place
    int y = 0;
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
            if(!sudoku[i][j])
            {
                y = 1;
                break;
            }
        if(y)
            break;
    }

    //check all values at that place
    n = findValues(sudoku, size, a, i, j);
    for(int l=0;l<n;l++)
    {
        //put value at vacant place
        sudoku[i][j]=a[l];
        //now solve the updated board
        Solve(size, stop);
    }

    sudoku[i][j]=0;
}
Nic
  • 12,220
  • 20
  • 77
  • 105
  • Can you provide us with some code that we can compile and run immediately (include main and stuff). But from looking at the code, in the memset in line 7, b is of size (size+1) but you're treating it as if it's of size (size*size) – AbdulRahman AlHamali Apr 22 '15 at 00:28
  • int s=(int)(sqrt(size)); this looks fishy to me, a 9x9 sudoku has size 9 or size 81 in you program? Also are you using backtracking or what are you trying to do? – maraca Apr 22 '15 at 00:30
  • ugh. Your variable names hurt my eyes :) – Michael Dorgan Apr 22 '15 at 01:08
  • You are leaking memory like crazy. Every new call in your Solve function needs a delete or delete[] to go with it. – Michael Dorgan Apr 22 '15 at 01:10

1 Answers1

1
int a[size+1];
memset(a,0,size*size*sizeof(int));

This will cause a segfault. You are clearing size squared of memory onto a normal size array.

This in addition to all your memory leaks created when you make you sudoku board. Your size+1 stuff also feels very hacky and not needed as well.

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61