9.6 Dynamic Memory Allocation
The run-time heap is an uninitialized area of data memory that is used for
dynamic memory allocation using the standard C library dynamic memory management functions,
calloc
, malloc
and realloc
along with
the C++ new operator. Most C++ applications will require a heap.
If you do not use any of these functions, then you do not need to allocate a heap. By default, a heap is not created.
In MPLAB X, you can specify a heap size in the project properties for the xc32-ld linker. MPLAB X will automatically pass the option to the linker when building your project.
If you do want to use dynamic memory allocation, either directly, by
calling one of the memory allocation functions, or indirectly, by using a standard C
library function that uses one of these functions, then a heap must be created. A heap is
created by specifying its size on the linker command line using the
--defsym=_min_heap_size
linker command line option. An example of
allocating a heap of 512 bytes using the command line is:
xc32-gcc foo.c -Wl,--defsym=_min_heap_size=512
An example of allocating a heap of 0xF000
bytes using the
xc32-g++ driver is:
xc32-g++ vector.cpp
-Wl,--defsym=_min_heap_size=0xF000
The linker allocates the heap immediately before the stack.