开发者

Stack Overflow Error java

开发者 https://www.devze.com 2023-03-11 13:33 出处:网络
I\'m trying to solve a problem that calls for recursive backtracking and my solution produces a stackoverflow error. I understand that this error often indicates a bad termination condition, but my te

I'm trying to solve a problem that calls for recursive backtracking and my solution produces a stackoverflow error. I understand that this error often indicates a bad termination condition, but my ternimation condition appears correct. Is there anything other than a bad termination condition that would be likely to cause a stackoverf开发者_开发问答low error? How can I figure out what the problem is?

EDIT: sorry tried to post the code but its too ugly..


As @irreputable says, even if your code has a correct termination condition, it could be that the problem is simply too big for the stack (so that the stack is exhausted before the condition is reached). There is also a third possibility: that your recursion has entered into a loop. For example, in a depth-first search through a graph, if you forget to mark nodes as visited, you'll end up going in circles, revisiting nodes that you have already seen.

How can you determine which of these three situations you are in? Try to make a way to describe the "location" of each recursive call (this will typically involve the function parameters). For instance, if you are writing a graph algorithm where a function calls itself on neighbouring nodes, then the node name or node index is a good description of where the recursive function is. In the top of the recursive function, you can print the description, and then you'll see what the function does, and perhaps you can tell whether it does the right thing or not, or whether it goes in circles. You can also store the descriptions in a HashMap in order to detect whether you have entered a circle.


Instead of using recursion, you could always have a loop which uses a stack. E.g. instead of (pseudo-code):

function sum(n){
  if n == 0, return 0
  return n + sum(n-1)
}

Use:

function sum(n){
  Stack stack
  while(n > 0){
    stack.push(n)
    n--
  }
  localSum = 0
  while(stack not empty){
    localSum += stack.pop()
  }
  return localSum
}

In a nutshell, simulate recursion by saving the state in a local stack.


You can use the -Xss option to give your stack more memory if your problem is too large to fix in the default stack limit size.


As the other fellas already mentioned, there might be few reasons for that:

  • Your code has problem by nature or in the logic of the recursion. It has to be a stoping condition, base case or termination point for any recursive function.
  • Your memory is too small to keep the number of recursive calls into the stack. Big Fibonacci numbers might be good example here. Just FYI Fibonacci is as follows (sometimes starts at zero):

    1,1,2,3,5,8,13,...

    Fn = Fn-1 + Fn-2

    F0 = 1, F1 = 1, n>=2


If your code is correct, then the stack is simply too small for your problem. We don't have real Turing machines.


There are two common coding errors that could cause your program to get into an infinite loop (and therefore cause a stack overflow):

  • Bad termination condition
  • Bad recursion call

Example:

public static int factorial( int n ){
    if( n < n ) // Bad termination condition
        return 1;
    else
        return n*factorial(n+1); // Bad recursion call
}

Otherwise, your program could just be functioning properly and the stack is too small.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号