Warning

You're browsing the old v2.x documentation. This version is no longer maintained. Click here to go the v3.x documentation.

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
proc [2019/05/23 09:13] – [Recursion] neilsproc [2019/06/13 20:55] – [Recursion] neils
Line 116: Line 116:
 When the compiler detects direct recursion, it generates code to save local variables and procedure parameters on the stack and restore them when the procedure returns. When the compiler detects direct recursion, it generates code to save local variables and procedure parameters on the stack and restore them when the procedure returns.
  
-**Be careful with recursion**. The C64's stack is very small, it's very easy to provoke a stack overflow. To estimate how big stack space you'll need, calculate how many bytes your local variables reserve (including the procedure params) and estimate the maximum number of recursive iterations. If you multiply the two values, you'll see how high the stack will grow by the last iteration. Always leave some stack space for normal operations as well (8-16 bytes in general, but this can be more depending on the context).+==== Prevent stack overflow ==== 
 + 
 +**Be careful with recursion**. The C64's stack is very small, it's very easy to provoke a stack overflow. To estimate how big stack space you'll need, you can use this formula: 
 + 
 +  (size of params + size of local vars + 2 byte address * possible number of iterations 
 + 
 +Always leave some stack space for normal operations as well (8-16 bytes in general, but this can be more depending on the context)
 + 
 +**Tip**: a good practice to prevent stack overflow is to move as many local variables to the global scope as possible