+ 4

[SOLVED] Segmentation fault

I'am writing a program that performs the knight's tour i.e. moves a knight around on a chessboard in such way that the knight visits every square exactly once. The program is a bit messy, I'll make it more concise and readable when I finish. For now it does just the first move, but putting it in a loop should do the rest. If I run it I can see the first move performed (that is a zero at the top left of the board) However it's not an allowed move for knights in chess. To fix that I need to add the condition "j != i" in the if at line 69. It should be: if( j != i && r + i < 8 && c + i < 8 ) But if I run the "fixed" program, the compiler gives me a segmentation fault. The only change I make is this "j != i" Any idea why I can't do that? https://code.sololearn.com/cXfBqgmoIRT3/?ref=app

31st Oct 2020, 3:49 PM
Davide
Davide - avatar
2 Antworten
+ 5
Using < instead of <= in the condition of the for loop on line 91 resolves the segfault. Maybe someone else can tell you why, I didn't bother much with the logic, sorry. Does that fix the issue?
31st Oct 2020, 5:34 PM
Shadow
Shadow - avatar
+ 2
Shadow OMG it's true! 😱😱😱 And it's also right to do for how I used the variable bestCount. But I clearly remember that I reasoned that it was right to put "<=" so I don't know if and when I would have checked there for the error. You are a saviour! Thank you so very much 🙏🙏🙏
31st Oct 2020, 5:55 PM
Davide
Davide - avatar