Stop Overflow means exactly what it says, since you had a recursive algorithm, it means that your memory stack (for function calls) is full. take a look at the call stack document to understand how the stack pointers, frames, and reverse pointers work:
When you created the iterative algorithm, you ran out of memory because the variables you stored are not in the call stack, but are stored in the memory of the function itself (in the same stack stack).
Of course, technically, both errors mean that you have no memory, but each of them happened in a different way. One of them is endless recursive method calls, and the other is memory overflow.
EDIT As for your edited question, I don't think stackOverflow can happen without an infinite loop or recursion if you don't have enough memory on your system. Maybe add more RAM?
source share