Showing results for 
Search instead for 
Did you mean: 

I am currently trying to understand memory allocation for stack, heap, and data

Associate II

In microcontroller programming, SRAM is typically divided by the compiler into sections for stack, heap, and data. Now, my question is whether the stack and heap sizes need to be set in the compiler or not. In Keil, I've seen these


Stack_Size		EQU     0x400

Stack_Mem       SPACE   Stack_Size

; <h> Heap Configuration
;   <o>  Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>

Heap_Size      EQU     0x200




Now, my question is if I have an array that is larger than 1024 kilobytes in the stack, do I need to change this value, or does the compiler handle it?

In general, what happens if our request exceeds the defined value in the compiler?

I would appreciate any explanations regarding this matter.


With Keil it does allocate RAM, so stack needs to be sufficiently large to cover all the auto/local variables, and call depth your code creates, plus whatever foot-print interrupts bring to the party.

If you overflow the stack, it will descend downward, thru the heap corrupting that, and then down thru the locales and the statics.


On GNU/GCC the stack is usually set to the top-of-ram (it decrements down before use), and the bottom of the stack defines the end of the heap space, which then extends to the statics.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..