malloc behaviour on an embedded system

前端 未结 5 1889
执笔经年
执笔经年 2020-12-09 02:17

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

5条回答
  •  春和景丽
    2020-12-09 02:46

    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.

提交回复
热议问题