one way may be to move all your local variables (the ones that otherwise end up on the stack) to a structure that you allocate dynamically on every entry to your recursive function and just keep a pointer to this structure as a local variable. This way you moved the required memory from the stack to the heap.
This technique will also work for the parameters. You would have to change the parameters of your function to just be a pointer to a structure containing the real values (be aware of the difference between passing by value and passing by reference though!).
Doing both you will end up with a recursive function that will only use a few bytes per invocation: 4 bytes for the parameter, 4 bytes for the return address, 4 bytes for the LINK #n,a6, 4 bytes for the local variable.
Naturally this has a performance impact since allocating/freeing the memory from/to the heap at every invocation can be a pretty big burden!
Frank
Richard Coutts wrote:
I'm using some recursive calls that are filling up my stack. There's no easy way around the recursion, so I'm finding other ways to be frugal with stack usage. I've been reading up on the stack issues and have a couple of questions:
(1) I read somewhere that even if I keep below the stack size that I allot myself, that Palm Hacks can occupy some of the stack, and that my app consequently may not have as much as it thinks it does. E.g., if my app is compiled for a 4K stack, but there are Hacks that occupy 512 Bytes, that I'm really working with a 3.5K stack (is this true? I'm not very savvy about how Hacks work). If this is the case, how do you work with this? Do you allocate the stack to be a little bigger than it needs to be to allow of Hacks to take some of it?
(2) When the stack overflows, I get the initial message from POSE listing the last 6 or so functions on the stack with the amount of stack they're using in parenthasis. But, there's usu. a dozen or so function calls above that that are the real culprits -- is "#pragma warn_stack_usage 1" the only way to get a function's stack size, or is there a way to see the amount of stack all of the functions were using at the time POSE crashed?
Thanks, Rich
-- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
