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

发布于 2024-08-28 17:47:36 字数 1663 浏览 5 评论 0原文

增加对象的实例变量是否会导致堆栈溢出错误?

例如:

此方法(java)将导致堆栈溢出错误:

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

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

但这会吗?:(.....是我为了缩短代码而放入的间隙。它已经足够长了。)

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]) ; 
    } 

}

以上目前不会导致堆栈溢出错误,但如果我在 main 中更改此行,我也能得到它吗: for (int i = 0; i<6000000; i++ ) 这样一来,不是有 600 万足够高的东西了吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

静谧 2024-09-04 17:47:36

堆栈溢出?不,但它可能会导致整数溢出,这是一个非常不同的事情。

堆栈溢出意味着方法调用堆栈上的空间已耗尽(可能是由于失控的递归调用)。如果增量超出其最大值,则整数溢出将导致 int 循环到其最低值。

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.

晒暮凉 2024-09-04 17:47:36

在Java中,堆栈溢出错误来自于过度的递归。这是函数直接或间接调用自身的地方。

在第一个示例中,StackOverflow 函数直接调用自身而不受限制。

在您的 Dice 示例中,没有函数调用自身的实例,因此您不太可能遇到堆栈溢出错误。

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.

记忆消瘦 2024-09-04 17:47:36

堆栈溢出错误是由无限递归(即方法调用自身次数过多)引起的。您的第二个代码示例似乎根本没有使用递归,因此我认为不可能出现堆栈溢出错误。

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.

凉薄对峙 2024-09-04 17:47:36

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

  • yes

简单地说:

当应用程序递归太深而发生堆栈溢出时,就会抛出堆栈溢出。
这意味着您的行 StackOverflow(x+1) ; 可能会引发堆栈溢出错误,具体取决于堆栈有多大。除此之外,代码将开始获取意外的 int 值。

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.

泅渡 2024-09-04 17:47:36

那么,您可以使用 -Xss 开关更改 java 中堆栈的最大大小。最小的堆栈约为 1KB,因此您不需要无限(甚至非常多)递归来获得所需的堆栈溢出,但您肯定需要比示例中给出的更多的堆栈。我想我的观点是,递归足以导致堆栈溢出,但不是必需的;使用任意小的调用堆栈,您可以通过任意少量的方法调用使其溢出。

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.

娇俏 2024-09-04 17:47:36

这将导致整数溢出,因为 int 类型从大约开始。 -2E7至2E7

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文