In most cases, the stack overflow occurs because the recursive method has not been defined, with a nonexistent or unattainable final condition, due to which the memory space of the stack will be exhausted. A correctly written recursion should not lead to a stack overflow.
However, there are situations where a method can create a stack overflow, even if it was correctly implemented. For example:
- Fast-growing (say, exponential) recursion. For example: a naive recursive implementation of the Fibonacci function
- Very large input, which ultimately leads to the exhaustion of the stack space.
Bottom line: it all depends on the specific case, it is impossible to generalize what causes the stack to overflow.
Óscar López
source share