多少个函数调用将导致堆栈溢出 [英] How many function calls will cause stack overflow

查看:147
本文介绍了多少个函数调用将导致堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当一个函数调用一个函数,该函数调用另一个函数时,有多少个调用(堆栈长度)会让我流入堆叠?是否有一般的经验法则?



我问的原因是因为我现在对于我的5个玩家卡片游戏来说更有效(设计明智) >

解决方案1:

  for(int i = 0; i <100; i ++){
p1.play();
p2.play();
p3.play();
p4.play();
}

解决方案2:

  p1.play(); //其中p1.play()调用p2.play()等等,直到p4再次调用p1。 
//这将继续100次

我喜欢解决方案2,所以如果有一个崩溃,我可以看到所有的函数调用从p = i = 0到p4在i = 100



但使用解决方案1,堆栈要短得多一个崩溃,我会看到在循环开始一个被称为函数play()发生崩溃



你建议什么?我知道1中有两个问题,但它们非常相关。



谢谢所有

解决方案根据我的经验,Java中的堆栈溢出几乎总是由于编程错误。请参阅典型尺寸此处



现在,你的第二个解决方案是,IMO,相当丑陋...几乎是一个编程错误。假设N = 100是你的游戏的持续时间,听起来只是错误的内存消耗(堆栈大小)随之增加。我根本不喜欢这个解决方案。


当有一个崩溃,我会看到在循环开始一个
称为功能play()发生崩溃


我没有看到真正的优势。为什么不放一个 try catch block,以便在崩溃的情况下可以打印出迭代号?


Hello android/Java developers,

When a function call a function and that function call another one and so on, how many calls (stack length) would get me into stack over flow? Is there a general rule of thumb?

The reason i am asking is because I am now which is more efficient(design wise) for my 5 players cards game

Solution 1:

for(int i=0;i<100;i++){
         p1.play();
         p2.play();
         p3.play();
         p4.play();
}

Solution 2:

   p1.play();    //where p1.play() calls p2.play() and so on until p4 calls p1 again.   
                 // this will go on for 100 times

I prefer solution 2 so if there is a crash I can see all the function calls from p1 at i=0 till p4 at i=100

but with solution 1, the stack is much shorter but when there is a crash I will see on the beginning of the loops a the called function play() where crash happened

What do you suggest? I know it is kinda 2 questions in 1 but they are very related

Thank you all

解决方案

In my experience, a stack overflow in Java is almost always due to a programming error. See typical sizes here.

Now, your second solution is, IMO, quite ugly... almost a programming error. Assuming N=100 is (sort of) the duration of your game, it sounds just wrong that the memory consumption (stack size) increases with it. I don't like that solution at all.

when there is a crash I will see on the beginning of the loops a the called function play() where crash happened

I don't see the real advantage. Why not put a try catch block so that in case of a crash you can print out the iteration number?

这篇关于多少个函数调用将导致堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆