__malloc_heap_start
and __malloc_heap_end
can be used to restrict the malloc() function to a certain memory region. These variables are statically initialized to point to __heap_start
and __heap_end
, respectively, where __heap_start
is filled in by the linker to point just beyond .bss, and __heap_end
is set to 0 which makes malloc() assume the heap is below the stack.__malloc_heap_end
must be adjusted accordingly. This can either be done at run-time, by writing directly to this variable, or it can be done automatically at link-time, by adjusting the value of the symbol __heap_end
.-Wl
options.__malloc_heap_end
is 0, the allocator attempts to detect the bottom of stack in order to prevent a stack-heap collision when extending the actual size of the heap to gain more space for dynamic memory. It will not try to go beyond the current stack limit, decreased by __malloc_margin
bytes. Thus, all possible stack frames of interrupt routines that could interrupt the current function, plus all further nested function calls must not require more stack space, or they will risk colliding with the data segment.__malloc_margin
is set to 32.__malloc_margin
will be considered if the heap is operating below the stack, or where __malloc_heap_end
will be verified otherwise.NULL
will eventually be returned to the caller.