Stack and Heap

Q

How can I change the size of Stack and Heap?

Q

What does the following error mean?

ld.exe: region RAM overflowed with stack

A

The sections .stack and .heap are user defined. In the startup, build and linker scripts provided, together with our newlibc example implementation, we provide you with an explanatory implementation of those.

Within the wscript in the Targets directory you are able to define the stack and heap size for each target individually.

Targets > NXHX > wscript
    bld.sdkcomponent(
        name            = "cmsis_configuration_sdk",
        export_defines  = [
                           "__STACK_SIZE = 0x00002000",
                           "__HEAP_SIZE =  0x00008000",
                           ],
        )

The heap itself is used in the newlibc implementation within the sbrk function. However, the heap area has to be initialized upfront. If you are using an operating system, you need to take care on how the stacks and heaps are used.

If you now receive the message: "ld.exe: region RAM overflowed with stack" it is because the defined size of stack and heap cannot be linked correctly.

In our examples, the heap is positioned after the sections containing .text, .rodata, .data, .bss and others containing compile time known data. The stack is always placed at the end of the selected memory. While the heap grows to the end of the memory, the stack grows from the end of it backwards in the direction of the heap.

Our example does not check the consistency of heap and stack during runtime, and it is possible, that stack and heap might overwrite each other.