Keil Logo

C166: How to check a User-Stack overflow

Information in this knowledgebase article applies to:

  • PK166 all versions


Are there ways to determine a user stack overflow during runtime? Why does the compiler/linker not warn me when the memory space needed for my local variables is bigger than the user stack area defined in the startup file.


Unlike for the system stack, C166 based microcontrollers do not have hardware protection mechanisms for the user stack. The user stack is a software implemented stack using R0 as a stack pointer.

During development, you can check the required user stack stack size as described in the C166 manual in chapter User Stack Size with a debugger (e.g. ULINK2).
You could also use a similar method during runtime in your application to watch the maximum stack allocation. Therefore you could fill the user stack with a constant value at the beginning of main(). Then you could periodically check the byte patters from the end of the user stack towards the beginning of the user stack. Please note that this method does not provide any protection against a program crash because of a user stack overflow.

The C166 Compiler cannot calculate the stack usage of your application program because it does not detect the actual program flow and can not know about the interrupt capabilities of your target system. The stack size is usually defined in the startup file but in applications with a real-time OS (e.g. RTX166-tiny), the user stack is defined per task. Therfore, the C166 Compiler does not even know the memory area and size which is reserved for the user stack.


Last Reviewed: Monday, November 26, 2012

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.