Stack Overflow error vs. Infinite loop

[亡魂溺海] 提交于 2019-12-22 08:14:58

问题


I know what an Infinite Loop error is. Is a stack overflow error the same thing. If not, what is the difference?

Can you give example code as well?


回答1:


If, instead of infinite loop, you have infinite (or very deep) recursion (function invoking itself), then you will get stack overflow. Whenever a function is invoked, some part of stack memory is consumed. Once all the stack is exhausted, you get - stack overflow error.




回答2:


These are not the same thing. Infinite loop error is dealing with iterative loops (no recursion), where as most stack overflow errors are dealing with recursion.

You should google "What is a stack overflow error":

The most common cause of StackOverFlowError is excessively deep or infinite recursion. In Java: There are two areas in memory the heap and stack. The stack memory is used to store local variables and function call, while heap memory is used to store objects in Java.



来源:https://stackoverflow.com/questions/27218736/stack-overflow-error-vs-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!