什么是StackOverflowError? [英] What is a StackOverflowError?

查看:336
本文介绍了什么是StackOverflowError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 StackOverflowError ,是什么原因,我应该如何处理?

What is a StackOverflowError, what causes it, and how should I deal with them?

推荐答案

参数和局部变量在堆栈上分配(对象的引用类型存在于堆上,变量引用该对象)。堆栈通常位于地址空间的上端,当它用完时,它朝向地址空间的底部(即朝向零)。

Parameters and local variables are allocated on the stack (with reference types the object lives on the heap and a variable references that object). The stack typically lives at the upper end of your address space and as it is used up it heads towards the bottom of the address space (i.e. towards zero).

您的进程还有一堆堆,它生活在你的进程的底端。在分配内存时,这个堆可以朝着地址空间的上端扩展。正如你所看到的,这个堆有可能与堆栈碰撞(有点像构造板!!!)。

Your process also has a heap, which lives at the bottom end of your process. As you allocate memory, this heap can grow towards the upper end of your address space. As you can see, there is a potential for the heap to "collide" with the stack (a bit like tectonic plates!!!).

堆栈溢出是一个坏的递归调用。通常,这是因为您的递归函数没有正确的终止条件,因此最终会永久调用它。然而,通过GUI编程,可以产生间接递归。例如,您的应用程序可能正在处理绘画消息,并且在处理它们时,它可能会调用一个使系统发送另一个绘画消息的功能。在这里,您没有明确地称呼自己,但是操作系统/虚拟机已经为您完成了。

The common cause for a stack overflow is a bad recursive call. Typically, this is caused when your recursive functions doesn't have the correct termination condition, so it ends up calling itself forever. However, with GUI programming, it's possible to generate indirect recursion. For example, your app may be handling paint messages, and, whilst processing them, it may call a function that causes the system to send another paint message. Here you've not explicitly called yourself, but the OS/VM has done it for you.

要处理它们,您需要检查代码。如果你有自己的功能,那么检查你是否有终止条件。如果你调用函数时调用函数,至少修改了一个参数,否则递归调用的函数将不会有可见的变化,终止条件是无用的。

To deal with them you'll need to examine your code. If you've got functions that call themselves then check that you've got a terminating condition. If you have then check than when calling the function you have at least modified one of the arguments, otherwise there'll be no visible change for the recursively called function and the terminating condition is useless.

如果你没有明显的递归函数,那么检查一下你是否调用任何函数间接地使你的函数被调用(像上面的隐含的例子)。

If you've got no obvious recursive functions then check to see if you're calling any library functions that indirectly will cause your function to be called (like the implicit case above).

这篇关于什么是StackOverflowError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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