cowman001, on 21 September 2012 - 01:01 AM, said:
how do i reset the stack? for future reference?
The stack is internal to lua, storing the current state of the program. You can't simply 'reset' it, short of restarting the computer. You can avoid overflowing it by making sure you return from any functions you call, in a timely manner.
It works something like this:
Every time you call a function (or start a new shell process), lua puts the current position in the program on the stack, then calls the function. Any parameters or local variables inside a function also go on the stack.
When you return from a function, it pops (removes from the stack) all the local variables, and returns to where the function was called from.
Unless you're very careful when you make recursive calls (functions calling themselves or their 'ancestors'), you continually push more data onto the stack, but never pop it back off by returning. Eventually you run out of stack memory, and your program crashes.
Consider the following badly-behaved example:
Spoiler
When run, it will rapidly call both functions in succession, adding to the stack each time, but never returning.
Output:
Spoiler
Because the functions never reach the 'end', they don't get a chance to pop off the data they added to the stack, so it keeps growing.
Let's have a look at what the stack looks like as we run the program:
Spoiler
In contrast, here's a well-behaved program:
Spoiler
When we first enter the function, it puts the return address on the stack, so the stack looks like:
funcOne() -- <return to line 10>
When we hit the end of the function, the return address gets popped, leaving the stack empty -- exactly as it found it.
Since we leave the stack in the same condition as it was when we started, it can never overflow.











