Proper Stack Length
JoeFL
Posts: 10
Hi,
A while ago, I came across a program on the parallax website for determining the stack usage of prop programs
running in cogs..
*****************************************
* Stack Length Demo v1.1 *
* Author: Jeff Martin *
* Copyright (c) 2006-2010 Parallax Inc. *
* See end of file for terms of use. *
*****************************************
I did what Jeff Martin suggested, at the early stages of programming, use a larger amount of stack space
than the program actually uses, and then when the program is complete, tweak the amount of stack space
using his Stack Length Demo program.
Thats what I did, and my program seems to be running fine after re-sizing my programs stack space.
Original Stack[350], Re-sized Stack[100]
My program 'seemed' to be running fine before I re-sized the stack space.
Question: How important or is it important at all to properly size stack space?
Thanks
Joe
A while ago, I came across a program on the parallax website for determining the stack usage of prop programs
running in cogs..
*****************************************
* Stack Length Demo v1.1 *
* Author: Jeff Martin *
* Copyright (c) 2006-2010 Parallax Inc. *
* See end of file for terms of use. *
*****************************************
I did what Jeff Martin suggested, at the early stages of programming, use a larger amount of stack space
than the program actually uses, and then when the program is complete, tweak the amount of stack space
using his Stack Length Demo program.
Thats what I did, and my program seems to be running fine after re-sizing my programs stack space.
Original Stack[350], Re-sized Stack[100]
My program 'seemed' to be running fine before I re-sized the stack space.
Question: How important or is it important at all to properly size stack space?
Thanks
Joe
Comments
A stack is a first in first out arrangement. And your conventional point of in and out is considered to be the Top of the stack. You want everything that goes in to come back out the top... not fall off the bottom.
If your code is storing return addresses and local variables off the end of it's allocated stack space then quite likely they will get overwritten by some other code.
When that happens all kind of weird things can occur which will have you scratching your head.
Normally I just "guestimate". A return address must be one long per call and I know how far my methods are calling each other in a nested way. Then add up how many local variables my methods have. Then add 100% on to that just to be safe. If things get weird try extending the stacks first.
If you want to be really sure use a stack checker program. Or use BST and inspect the listing output to see what the code really needs for a stack.
But after a bit of study of Forth, the use of one for all the 'temporary variables' seems quite sensible. I have suspicions that Chip Gracey might even have been heavily influenced by Forth at one time or another.
Still, one really does have to do some testing to make sure the stack is deep enough. Being told that 'wierd things may happen' is not very emperical.
Now that I know that I can read the code in BST to see what's up, I will try that.
Correct, saying 'weird things may happen' is not very empirical.
My feeling is that most of us kick off writing code with "a bunch of stack that should be OK", size determined by sticking a finger in the air and feeling the wind. As we go along code gets bigger and bigger, nesting levels get deeper and deeper until eventually "weird things" happen.
Having been there many times before one of the first things that comes to mind is, "perhaps I need more stack now" and we look into it.
Or perhaps he whole program starts to not fit into memory and we start to look into shrinking stacks. That's when those source listings or stack checking tools help a lot.
Merely speculating.... Forth is a very good example of the utility of stacks. Please read the banner below my name in this posting.
Can I borrow you banner?