View Single Post
Posts: n/a
Default 04-24-2010, 07:51 AM

Originally Posted by x2495iiii View Post
The second one. Alice is infamous for getting clunky and slow when too many complex infinite loops are used.
This isn't a even really a bug. Unless you really know what you're doing, you shouldn't ever use infinite recursion.

In short, there's a special bit of memory called the Call Stack that every program gets. The call stack keeps track of what the program is doing, what methods called what, so the program knows when to hand control to what bit of itself.

Imagine you have a ball of red thread - that's your call stack. Every time you talk to a friend, you hold the end of the string and hand them the ball. If you finish your coversation, the friend hands you the ball back. If you get interrupted, you friend holds onto a bit of string, hands the ball to the person that interrupted him, and they start talking. Then there's another interruption, and the string gets passed again.

The first case is a normal method call. The second is a recurive one. If your friends keep interrupting one another, eventually you'll run out of red string to trace the conversation back to how it started. This is a stack overflow, which is why Alice crashes when you use infinite recursion.

In general, if you can write a method just as well (or even almost as well) without using recursion, you should. A computer will always be better at a while loop than a recursive method.

Last edited by flats; 04-24-2010 at 07:55 AM.
Reply With Quote