问题
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