Home > front end >  Why doesn't a forever loop crash? Why does an infinite recursion crash?
Why doesn't a forever loop crash? Why does an infinite recursion crash?

Time:05-15

My question is: Why does an infinite recursion crash, but not an infinite loop? They both feel like normal forever iterators yet one crashes while the other doesn't. I am looking for the hardware/low-level response. I usually get stackoverflow error when experimenting this on Jupyter with Python. Also, when I mean low-level response, I mean what happens in the computer hardware(RAM, CPU) that causes the infinite recursion to crash, but not the infinite iteration.

CodePudding user response:

Because an infinite recursion keep storing recursive calls in the stack (which is one of the memories held by your application), so it will eventually fill the stack completely, hence you get the classic stack overflow error.

In an infinite loop, you don't store anything and frequently you just keep updating variables and things like that, which won't occupy more memory than these variables already do, so nothing will break. However, you can have an infinite loop breaking if you add things in the memory inside your loop, like adding an element to a list inside the loop.

  • Related