2025-09-18 11:49 PM - last edited on 2025-09-19 12:32 AM by mƎALLEm
My application use lots of RAM, I'm concern about the stack memory.
The RAM size is 128KB,from 0x20000000 to 0x20020000.
Form the linker script (.ld) file,
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
Use "Build Analyzer" to check Memory Details, it says "._user_heap_stack Run Address 0x2000d160 1.5K ".
But when I run the debugger, SP starting at 0x20020000; when call functions it goes to 0x2001ffe8; 0x2001ffe0...
My question are:
1. The ._user_heap_stack Run Address should be 0x2001FA00 (size 0x400 + 0x200 ==>0x600), end at 0x20020000.
2. During debugging, I can use SP to view the stack pointer address. How can I view Heap address?
3. Stack area should be from 0x2001FC01 to 0x20020000; and heap area is from 0x2001FA00 to 0x2001FC00?
If stack overflow, it will corrupt heap area data?
2025-09-19 1:57 AM
Linker inly reserves define area for stack, in startup file initial stack value is set to end of RAM memory.
Yes when stack wil overflow it will corrupt heap, bot only when full heap will be used,
2025-09-19 7:57 AM
The heap starts at the "_end" symbol, which is at the end of the global memory in use. The heap includes all unused memory so it's typically quite a bit larger than the _Min_Heap_Size value.
2025-09-19 8:16 AM - edited 2025-09-19 8:24 AM
If you use the sysmem.c provided with the CubeIDE toolchain: the heap end address is the local variable __sbrk_heap_end in sysmem.c.
Per the "old good" naive memory layout, the stack goes from the end of the RAM down to the end of the heap. And the heap grows up until it hits the (current) SP. You may prefer a more robust layout where the stack size is fixed and located at the *bottom* of the RAM. Then overflowing the stack should cause exception. This is in line with allocation of fixed-size stacks in RTOSes. The heap will reside at the top of the RAM and can expand to the RAM end. Again, access above the end address should raise exception.