Shop OBEX P1 Docs P2 Docs Learn Events
In C, how do you calculate stack_size for cogstart ? — Parallax Forums

In C, how do you calculate stack_size for cogstart ?

twm47099twm47099 Posts: 867
edited 2014-01-02 06:49 in Propeller 1
Sorry for the cross post - I posted this originally in the Learn forum and got no response. Maybe in the c forum?? I added a little more info here. I guess a related question that might help me figure out how to size it is what is the stack used for in the new cog?


In the Propeller C - Functions Tutorial Multicore Example, the General Multicore Setup has the following statements regarding stack size:

"Declare a stack array with 40 longs (required) + extra for local variables and calculations. Be liberal with the extra memory, and use testing to pare it down after your prototyping is done.
Example: unsigned int stack[40 + 25]; "

I've just used stack[50 + 50]; so far and it has worked for my application, but --

Are there general rules to calculating stack size (based on things like number and type of parameters, variables, function calls, if-statements, etc.)? Is there C-code to evaluate a cog's stack needs once the program is running (like there are for Spin programs)? Or is the method of testing simply trial and error (start big and keep cutting until something bad happens)?

Thanks,
Tom

I appreciate the information. Thanks again.
Tom

Comments

  • ersmithersmith Posts: 6,054
    edited 2014-01-02 06:49
    The general rule of thumb would be to allocate the space needed for local variables, parameters, and return address in the longest call chain that could ever be active; that is, if you have function A that has 3 int local variables, which calls functions B (1 double local variable, 2 int parameters) and C (1 int local variable, no parameters) then you need the space for A (3 longs) and B (4 longs: a double is 2 longs), plus 2 longs for the return addresses for A and B. It's best to err on the side of caution and add more if you're at all in doubt. If you want an exact value you can look at the compiler's assembly output.
Sign In or Register to comment.