William Chang

how to setting up the stack and heap in KEIL (microLib)

Blog Post created by William Chang on May 13, 2018

Keep in mind.

To use microlib, you must specify an initial pointer for the stack. You can specify the initial pointer in a scatter file 

or using the __initial_sp symbol.


To use the heap functions, for example, malloc() , calloc() , and free() you must specify the

location and size of the heap region.



To configure the stack and heap for use with microlib, use either of the following methods:

method 1

- Define the symbol __initial_sp (must be aligned to a multiple of 8 bytes) to point to the top of the stack. (If using the heap; also define symbols __heap_base and __heap_limit (must point to the byte beyond the last byte in the heap region))


method 2

In a scatter file; either:

- Define ARM_LIB_STACK and ARM_LIB_HEAP regions. (If you do not intend to use the heap, only define an ARM LIB region.)

- Define an ARM_LIB_STACKHEAP region. (If you define an ARM_LIB_STACKHEAP region, the stack starts at the top of that region. The heap starts at the bottom)


Let's take examinations.

step1: choose Run-time memory models

One-region model or Two-region model


/* If you are using Micro C lib, the linker will ignore the Heap parts (I am not very sure, but my projects always ignore heap parts when using micro c lib, and I haven't tested the Two region model yet, I will get that done in the future)*/


In my case, I use the one-region model.


step2: Choose the setting up method. 

In my case,I use method 1- Define the symbol __initial_sp

That's it~