简体   繁体   中英

Will this ever result in a stack overflow error?

Will incrementing the instance variables of an object ever lead to a stack overflow error?

For example:

This method (java) will cause a stack overflow error:

class StackOverflow {
    public static void StackOverflow (int x) 
    {
        System.out.println (x) ; 
        StackOverflow(x+1) ; 
    } 

    public static void main (String[]arg) { StackOverflow (0) ; 
} 

but will this?: (..... is a gap that i've put in to shorten the code. its long enough as it is.)

import java.util.*;
class Dice 
{ 
    String name ; 
    int x ; 
    int[] sum ;  

....

public Dice (String name) 
{ 
    this.name = name ; 
    this.x = 0 ; 
    this.sum = new int[7] ; 
}

....

public static void main (String[] arg) 
{
    Dice a1 = new Dice ("a1") ; 
    for (int i = 0; i<6000000; i++) 
    {
        a1.roll () ;
        printDice(a1) ; 
    } 
}

....

    public void roll () 
    {
        this.x = randNum(1, this.sum.length) ; 
        this.sum[x] ++ ;
    }

    public static int randNum (int a, int b) 
    {
        Random random = new Random() ;
        int c = (b-a) ;
        int randomNumber = ((random.nextInt(c)) + a) ;
        return randomNumber ;
    }

    public static void printDice (Dice Dice) 
    { 
        System.out.println (Dice.name) ; 
        System.out.println ("value: "+Dice.x) ; 
        printValues (Dice) ; 
    } 

    public static void printValues (Dice Dice) 
    { 
        for (int i = 0; i<Dice.sum.length; i++) 
        System.out.println ("#of "+i+"'s: "+Dice.sum[i]) ; 
    } 

}

The above doesn't currently cause a stack overflow error but could i get it too if i changed this line in main: for (int i = 0; i<6000000; i++) so that instead of 6 million something sufficiently high were there?

Stack overflow? No, but it could lead to an integer overflow which is a very different thing.

A stack overflow means that space on the method invocation stack is exhausted (possibly because of a runaway recursive call). An integer overflow will cause the int to circle around to its lowest value if incremented beyond its maximum value.

In Java, a stack overflow error comes from excessive recursion . This is where a function calls itself , either directly or indirectly.

In your first example, the StackOverflow function directly calls itself without bound.

In your Dice example, there is no instance where a function calls itself, so you are not likely to encounter a stack overflow error.

A stack overflow error is caused by infinite recursion, that is, a method calling itself too many times. Your second code example doesn't seem to use recursion at all, so I don't think a stack overflow error is possible.

Well, you can change the maximum size of a stack in java with the -Xss switch. The smallest stack is about 1KB, so you wouldn't need infinite (or even very much) recursion to get the desired stack overflow, but you'd definitely need more than you've given in your example. I guess my point is that recursion is sufficient, but not necessary, to cause stack overflow; with an arbitrarily small call stack you could overflow it with an arbitrarily small number of method calls.

Will this ever result in a stack overflow error?

  • yes

Simply put:

Stack overflow is thrown when a stack overflow occurs because an application recurses too deeply. That means your line StackOverflow(x+1) ; can throw a stack overflow error depends on how big your stack is. Apart from that the code will start getting unexpected int values.

This will lead you to Integer overflow because int types goes from approx. -2E7 to 2E7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM