malloc()总是在ChibiOS中返回NULL [英] malloc() always returns NULL in ChibiOS

查看:426
本文介绍了malloc()总是在ChibiOS中返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ChibiOS 应用程序,通过 malloc()使用动态内存分配,

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

但是,我观察到100%的时间我调用 malloc(),它返回 NULL 。我已经确认:

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


  • 微控制器内存未完整

  • 错误也发生在大小-1 malloc调用,所以内存块大小不是问题的原因。

  • errno 总是 malloc()调用

  • 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

中的ENOMEM >如何解决这个问题?

How can I resolve this issue?

推荐答案

当您查看 _sbrk os / various / syscalls.c 中,您可以清楚地看到它总是返回一个 ENOMEM 错误如果 CH_CFG_USE_MEMCORE == FALSE

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.

除非您设置 CH_CFG_USE_MEMCORE = TRUE chconf.h 中,ChibiOS核心内存管理器已完全禁用, _sbrk 和其他与内存相关的功能仅包含在目标文件中,因此不会出现链接错误。

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.

为了正确配置ChibiOS,请确保以下内容设置在 chconf.h 中:

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

#define CH_CFG_USE_MEMCORE                  TRUE

为避免出现可靠性问题,您可能希望使用内存池或替代算法,而不是在可能的情况下。有关为什么 malloc()经常出现的详细说明,请参阅详细说明嵌入式系统的一个坏主意(实际上绝大多数嵌入式编码标准是完全禁止的)。

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).

这篇关于malloc()总是在ChibiOS中返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆