我有一个STM32F103VCT6
带有 48kb SRAM 的微控制器,最近我遇到了内存冲突:
我有一些静态变量(让我们调用它A
)位于堆中,大小为0x7000
,我编写了一些简单的函数来获取有关堆栈和堆的信息:
void check(int depth) {
char c;
char *ptr = malloc(1);
printf("stack at %p, heap at %p\n", &c, ptr);
if (depth <= 0) return;
check(depth-1);
}
所以我得到了这样的东西:
stack at 2000939b, heap at 20008fd0
stack at 20009383, heap at 20008fe0
stack at 2000936b, heap at 20008ff0
stack at 20009353, heap at 20009000
stack at 2000933b, heap at 20009010
stack at 20009323, heap at 20009020
stack at 2000930b, heap at 20009030
stack at 200092f3, heap at 20009040
stack at 200092db, heap at 20009050
stack at 200092c3, heap at 20009060
stack at 200092ab, heap at 20009070
所有静态变量(包括A
)都已经获得了它们的堆,所以堆位于0x8fd0
. 看起来,最初,堆栈指针位于0x939b
,远离 48kb ( 0xc000
)
当我将A
变量大小更改为时,0x4000
我得到了这张图片:
stack at 2000639b, heap at 20005fd0
stack at 20006383, heap at 20005fe0
stack at 2000636b, heap at 20005ff0
stack at 20006353, heap at 20006000
stack at 2000633b, heap at 20006010
stack at 20006323, heap at 20006020
stack at 2000630b, heap at 20006030
stack at 200062f3, heap at 20006040
stack at 200062db, heap at 20006050
stack at 200062c3, heap at 20006060
stack at 200062ab, heap at 20006070
因此,似乎堆栈位置不在 SRAM 的末尾,而是在某种程度上依赖于用户定义的变量。
如何将堆栈对齐到 SRAM 的最后(48kb)?
我正在使用带有GNU Tools ARM Embedded
工具链的 CooCox IDE。
谢谢!
编辑:
对不起,这里有一些误解,A
不是 const,我只因为关键字而称它为静态:
static uint8_t A[A_SIZE];
printf("A is at %p\n", &A);
这表明A
位于内存的开头:
A is at 20000c08