我目前正在开发一个嵌入式项目(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(堆) …