11.8 Heap Allocation
The MPLAB XC32 compiler standard C library
supports dynamic memory allocation functions such as malloc()
and free()
. Applications which
utilize these functions must instruct the linker to reserve a portion of data memory for
this purpose. The reserved memory is called a heap.
Applications can specify the heap size by using the --heap
command option. For example:
xc32-ld -o t.exe t1.o --heap=0x100
While performing automatic heap allocation, the linker allocates the heap from unused data memory. The heap size is always specified by the programmer. In contrast, the linker sets the stack size to a maximum value, utilizing all remaining data memory.
As an alternative to automatic heap allocation, the heap may be allocated directly with a user-defined section in assembly source code. For example:
.section my_heap, heap
.space 0x100
The location and size of the heap are reported in the link map output file, under the heading Dynamic Memory Usage. If the requested size is not available, the linker reports an error.