Prevent Stack Overflow

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

There were multiple discussions related to Stack overflow.

   

Here are some of the tips to have a good code -

   

1. CASE statements are not the most efficient coding practice. This leads to increased use of stack space to store the interim results of each possible SWITCH state.

   

2. IF statement with several decision points results in a byte being pushed to the stack for each comparison. Wherever  possible, replace this type of statement with the simplest form possible.

   

3. Each passed variable is pushed to the stack for storage during a function call in C. Therefore, limit the number and size of passed variables to reduce stack usage. One way to  do this is to use global variables.

   

4. Each local variable used by a function is allocated a byte on the stack on entry into the function. As a result, care is to be taken limit the number of local variables used by a function.

0 Likes
3 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

To reduce stack size do not declare local variables in main(). Since main() is a function they would be allocated on the stack.

   

But

   

The stack-size of a PSoC5 can be set at compile-time and so can be increased as needed and is set initially to 16K!!!. The problem of stack overflow in a PSoC5 environment is minimal if not non-existing (as long as the program behaves "normal").

   

Stack overflow is more a problem for PSoC1 or PSoC3 which both have a stacksize of 256 bytes only and is not adjustable.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

A useful reference -

   

 

   

http://www.embedded.com/design/prototyping-and-development/4025013/Say-no-to-stack-overflow

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 These are few good coding practices which are to be followed..............

0 Likes