malloc() always returns NULL in ChibiOS

ε祈祈猫儿з 提交于 2019-12-25 06:47:01

问题


I have a ChibiOS application where I'm using dynamic memory allocation via malloc().

However, I observed that 100% of the time I call malloc(), it returns NULL. I have confirmed that:

  • The microcontroller memory is not full
  • The error also occurs for size-1 malloc calls, so the memory chunk size is not the cause of the issues.
  • errno is always ENOMEM after the malloc() call

How can I resolve this issue?


回答1:


When you look at the definition of _sbrk in os/various/syscalls.c, you can clearly see that it always returns a ENOMEM error if CH_CFG_USE_MEMCORE == FALSE.

Unless you set CH_CFG_USE_MEMCORE = TRUE in chconf.h, the ChibiOS core memory manager is disabled completely and _sbrk and other memory-related functions are only included in the object files so no linking errors occur.

In order to properly configure ChibiOS correctly, ensure that the following is set in chconf.h:

#define CH_CFG_USE_MEMCORE                  TRUE

In order to avoid running into reliability issues, you might want to use memory pools or alternative algorithms instead where possible. See this detailed explanation for a description why malloc() is often a bad idea on embedded systems (it's actually forbidden in most embedded coding standards entirely).



来源:https://stackoverflow.com/questions/32446905/malloc-always-returns-null-in-chibios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!