NOTICE: The Processors Wiki will End-of-Life on January 15, 2021. It is recommended to download any files or other content you may need that are hosted on processors.wiki.ti.com. The site is now set to read only.
Stack and Heap size requirements
Introduction[edit]
Stack overflows and insufficient heaps can cause a variety of software problems like runtime crashes and instability. A common question that is asked is: how to determine the amount of stack and heap space required by the application and allocate it optimally?
Stack usage[edit]
The stack is where memory is allocated for automatic variables within functions. Please see: http://processors.wiki.ti.com/index.php/Code_Generation_Tools_FAQ#Q:_What.27s_the_difference_between_HEAP_and_STACK.3F
Stack overflows can cause software instability and runtime crashes, so it is important to make sure that sufficient stack space is allocated for the application. The general recommendation to determine amout of stack required is to fill the stack with a known value (can be done in linker command file by filling the .stack section), let the program run and see how much of the stack was altered by viewing it in a memory window. This should tell how much stack was used and accordingly, the stack size can be reduced or increased as needed.
Stack size can be set in CCS4 under Project->Properties->C/C++ Build->Linker->Basic Options, under --stack_size
To statically (i.e. without running the code) examine how much stack is needed, the call_graph tool from the cg_xml package can be used. http://processors.wiki.ti.com/index.php/Code_Generation_Tools_XML_Processing_Scripts
More information related to using the call_graph tool can be found here: http://processors.wiki.ti.com/index.php/Stack_issues#Finding_out_static_stack_usage
To dynamically check for stack overfow during runtime, watchpoints can be used. See this page for more information:
http://processors.wiki.ti.com/index.php/Checking_for_Stack_Overflow_using_Unified_Breakpoint_Manager
For MSP430, CCS4 has a built-in option under View->Breakpoints, to set a Break on Stack overflow, which will cause program execution to stop on the instruction that caused the stack overflow. Details are in the CCS v4.2 Users Guide for MSP430, http://www.ti.com/lit/slau157 as well as the application note "Advanced Debugging using the Enhanced Emulation Module", http://www.ti.com/lit/slaa393
For Stellaris/Tiva devices , see this page for how to check for stack overflow.
For C28x, see this page for how to check for stack overflow.
Heap usage[edit]
The heap is a large pool of memory that can be allocated at runtime by application programs. Memory is allocated from a global pool, or heap, that is defined in the .sysmem section. Please see: http://processors.wiki.ti.com/index.php/Code_Generation_Tools_FAQ#Q:_What.27s_the_difference_between_HEAP_and_STACK.3F
The heap is dependent on the amount and type of data used for dynamic allocation (malloc, calloc, printf, etc.), therefore there is no mechanism to precisely determine the heap usage (unless using an RTOS like BIOS). It is recommended to start with a reasonable heap size like 0x400 when there is limited dynamic allocation (like printf() calls in the code), and increase it as needed depending on the application.
Heap size can be set in CCS4 under Project->Properties->C/C++ Build->Linker->Basic Options, under --heap_size
Additional details about the console I/O functions (printf, scanf, etc.) can be found in http://processors.wiki.ti.com/index.php/Tips_for_using_printf