0

I have a recursive method that I am fairly certain is finite. However, when I run it I receive a stack overflow error. Is there another possible way to get stack overflow that I happen to be doing or what is wrong with the method?

public static void solve(int row, int column){
    if (row<=8){
        if (column>8){
            solve(row+1, 0);
        }
        if (row<=8 && column<=8 && (Rows[row][column]==0)){
            for (int a = 1; a<=9;a++){
                if (check(row, column, a)==false&&Rows[row][column]!=a){
                    Rows[row][column]=a;
                    break;
                }
            }
        }
        solve(row, column+1);
    }
}
6
  • 5
    Is Stack OverFlow Error possible in finite recursion? Yes. See also the Ackermann function. Commented Jan 8, 2016 at 3:26
  • 2
    Yes it is quite possible in "finite" recursion -- since it is easy to write a program that does finite recursion, but will run out of most common sizes of stack memory, a very finite resource. Commented Jan 8, 2016 at 3:26
  • 1
    Sorry, I'm currently in a hurry, so I can't write an answer about your problem, but you want if (column>8) solve(row+1, 0); else solve(row, column+1);. The single line solve(row, column+1); in your code should only be used if column>8 is false. Commented Jan 8, 2016 at 3:34
  • I'm pretty sure there's a bug in your code, and that your method is in fact infinitely recurring. Commented Jan 8, 2016 at 3:42
  • 1
    @Tom Thanks so Much. Changing that reduced the memory and it works fine now. Commented Jan 8, 2016 at 3:58

1 Answer 1

2

Stack size in JVM is limited so it is possible to get StackOverflow with finite recursion or even without any recursion.

You can increase JVM stack size using -Xss option:

java -Xss16M YouMainClass
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.