Shop OBEX P1 Docs P2 Docs Learn Events
Proper Stack Length — Parallax Forums

Proper Stack Length

JoeFLJoeFL Posts: 10
edited 2013-10-15 06:06 in Propeller 1
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

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-10-14 10:47
    Less than adequate stack space will have data fall off the bottom of the stack.

    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.
  • Heater.Heater. Posts: 21,230
    edited 2013-10-14 11:22
    It is very important to have enough stack space.
    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.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-10-14 11:34
    Prior to getting into Forth on the Propeller, I thought the Stack is Spin was a rather odd concept.

    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.
  • Heater.Heater. Posts: 21,230
    edited 2013-10-15 01:21
    What has Forth got to do with it? Pretty much every high level language since the dawn of time has used a stack for return addresses and local variables etc. Assembly language programmers were using a stacks to do that long before.

    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.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-10-15 05:55
    @Heater
    Merely speculating.... Forth is a very good example of the utility of stacks. Please read the banner below my name in this posting.
  • Heater.Heater. Posts: 21,230
    edited 2013-10-15 06:06
    Loopy,

    Can I borrow you banner? :)
Sign In or Register to comment.