简体   繁体   English

这会导致堆栈溢出错误吗?

[英]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: 此方法(java)将导致堆栈溢出错误:

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? 上面的当前当前不会导致堆栈溢出错误,但是如果我在main中更改了这一行,我也能得到它吗: for (int i = 0; i<6000000; i++)所以那里不是600万的足够高的东西?

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. 如果整数溢出超过其最大值,则整数将导致int绕到其最小值。

In Java, a stack overflow error comes from excessive recursion . 在Java中,堆栈溢出错误来自过度的递归 This is where a function calls itself , either directly or indirectly. 这是函数直接或间接调用自身的地方。

In your first example, the StackOverflow function directly calls itself without bound. 在您的第一个示例中, StackOverflow函数直接无限制地调用自身。

In your Dice example, there is no instance where a function calls itself, so you are not likely to encounter a stack overflow error. 在您的Dice示例中,没有函数调用其自身的实例,因此您不太可能遇到堆栈溢出错误。

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. 好了,您可以使用-Xss开关更改java中堆栈的最大大小。 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. 最小的堆栈约为1KB,因此您不需要无限(甚至非常多)的递归来获得所需的堆栈溢出,但是您肯定需要比示例中给出的更多的内容。 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) ; 那意味着你的行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. 除此之外,代码还将开始获取意外的int值。

This will lead you to Integer overflow because int types goes from approx. 这将导致您出现Integer溢出,因为int类型从大约。 -2E7 to 2E7 -2E7至2E7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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