相关疑难解决方法(0)

嵌入式系统上的malloc行为

我目前正在开发一个嵌入式项目(STM32F103RB,CooCox CoIDE v.1.7.6 with arm-none-eabi-gcc 4.8 2013q4),我试图了解当RAM满时如何malloc()表现C.

我的STM32有20kB = 0x5000Bytes的RAM,0x200用于堆栈.

#include <stdlib.h>
#include "stm32f10x.h"

struct list_el {
   char weight[1024];
};

typedef struct list_el item;

int main(void)
{
    item * curr;

    // allocate until RAM is full
    do {
        curr = (item *)malloc(sizeof(item));
    } while (curr != NULL);

    // I know, free() is missing. Program is supposed to crash

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

一旦堆太小而无法分配,我希望malloc()能够返回NULL:

0x5000(RAM) - 0x83C(bss) - 0x200(堆栈)= 0x45C4(堆) …

c malloc memory-management out-of-memory stm32

28
推荐指数
3
解决办法
2万
查看次数

标签 统计

c ×1

malloc ×1

memory-management ×1

out-of-memory ×1

stm32 ×1