Basically, I have to write a basic program that solves the n-queen problem, which I have done, but it throws a segmentation fault if I input any number >=11.
From what I have read online, this error is usually caused by faulty logic when dealing with memory, but I can’t seem to figure out what I have done wrong.
void generateBoard(int board[],int column,int length,int count)
{
if(column == 0 && board[0]<length) //prevents outputting the results infinitely
{
++board[0];
generateBoard(board, ++column, length, count);
}
else
{
bool lineNotFound = true;
int row = board[column];
while(lineNotFound && row < length)
{
++row; //temporary value for a column value candidate
lineNotFound = false;
for(int i = 0; i < column && !lineNotFound; ++i)
{
if(board[i] == row || (board[i]+column-i) == row || (board[i]-column+i) == row) // check diagonal and horizontal
{
lineNotFound = true;
}
else
{
board[column] = row;
}
}
}
if(column == length-1 && !lineNotFound) // at last column and valid board
{
output(board,length,++count);
generateBoard(board,column,length,count);
}
else if(!lineNotFound) // not at last column, but valid position found
{
generateBoard(board,++column,length,count);
}
else if(column != 0) //no valid columns, go back a step
{
board[column] = 0;
generateBoard(board,--column,length,count);
}
}
}
I realize that is a big chunk of code, but I think it’s necessary to post it all to get an idea of the problem.
Any ideas? :s
I’m new to programming c++, so I don’t know where to start debugging this.
Start debugging at the beginning and then let it run till the segmentation fault. When the fault occurs – look at the stack. I would guess that you’re exceeding your stack with the recursion – that’s the main problem with recursion programs.
You can enlarge your stack and then the fault will occur not with input 11 but with some other number, but recursive programs will always crash on stack issues with the input large enough.
By the way – make sure your recursion is bounded, i.e.: at some point your function should exit without calling itself further. IMHO, this is better be done at the beginning (although its a matter of style and convenience), because then you’ll see clearly the condition upon which the recursion ends and it will be easier to debug infinite recursions (which also cause seg faults because of stack exhaustion). In your case it is not immediately clear how the recursion ends, and I wouldn’t be surprised that it doesn’t for some inputs.
clarification
While on some systems you get a “Stack Overflow” error, on others you would get “Segmentation Fault” for the same thing. I’m guessing you’re on one of the “others”.
To show that, I just compiled and ran this code:
On my GCC/Ububtu machine. This program has infinite recursion, which ends up with “segmentation fault” crash.
You can convert any recursive algorithm into iterative. Instead of recursively calling the function with new variables, use the STL
std::stackto push and pop them and run in a loop. Here the details : http://www.cplusplus.com/reference/stl/stack/