11.12 Dynamic Memory Allocation

The C 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. 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.

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 input/output function, then a heap must be created. A heap is created by specifying its size on the linker command line, using the --heap linker command-line option. An example of allocating a heap of 512 bytes using the command line is:

xc-dsc-gcc -T pdevice.gld foo.c -Wl,--heap=512

The linker allocates the heap immediately below the stack.

You can use a standard C library input/output function to create open files (fopen). If you open files, then the heap size must include 40 bytes for each file that is simultaneously open. If there is insufficient heap memory, then the open function will return an error indicator. For each file that should be buffered, 4 bytes of heap space is required. If there is insufficient heap memory for the buffer, then the file will be opened in unbuffered mode. The default buffer can be modified with setvbuf or setbuf.