I\'m currently working on an embedded project (STM32F103RB, CooCox CoIDE v.1.7.6 with arm-none-eabi-gcc 4.8 2013q4) and I\'m trying to understand how malloc()
b
The arm-none-eabi-*
toolchain distribution includes the newlib C library. When newlib is configured for an embedded system, then the user program must provide an _sbrk() function for it to work properly.
malloc()
relies solely on _sbrk()
to figure out where the heap memory starts, and where it ends. The very first call to _sbrk()
returns the start of the heap, and subsequent calls should return -1
if the required amount of memory is not available, then malloc()
would in turn return NULL
to the application. Your _sbrk()
looks broken, because it apparently lets you allocate more memory than there is available. You should be able to fix it so that it returns -1
before the heap is expected to collide with the stack.