views:

184

answers:

6

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?

+9  A: 

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.

Kris
What is an integer overflow?
David
It's when the value of an integer becomes too large for the type to hold. For an `int` (a signed 32-bit integer), it's about two thousand million.
Matti Virkkunen
I have a new idea for a website name.
Jack Leow
@David: specifically in java, an int can hold any value between -2,147,483,648 and 2,147,483,647, inclusive. If you try to store a larger number, it is called an integer overflow. If you try and store a smaller number, it is called an integer underflow.
Jason Coco
I don't think "Integer Overflow" has quite the same ring to it...Maybe "Underflow", but that sounds gross AND disappointing. =P
Chris Cooper
@Jack, you're too late: http://integeroverflow.com
BalusC
+3  A: 

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.

Greg Hewgill
what is meant by bound?
David
A certain condition under which the function doesn't call itself anymore. Recursion is perfectly OK, but recursing too many levels deep is not.
Matti Virkkunen
Without there being a condition on which the recursion stops.
Aram Hăvărneanu
+2  A: 

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.

Matti Virkkunen
A: 

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.

Jono
A: 

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.

ring bearer
A: 

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

flopex
Not true, int can hold values between -2^31 = -2,147,483,648 and 2^31 - 1 = 2,147,483,647 (inclusive).
Toon Van Acker