我正在使用一个在.h中声明uint32_t并在.c中定义它的库。程序无法读取变量并崩溃。
宣言:
extern uint32_t SystemCoreClock;
定义:
uint32_t SystemCoreClock = 4000000;
使用变量:
uint32_t tickNum=123, *ptr1=NULL, *ptr2=NULL; // added by me to debug
ptr1 = &tickNum; // added by me to debug
ptr2 = &SystemCoreClock; // added by me to debug
tickNum = SystemCoreClock; // added by me to debug
然后我用gdb检查地址。
执行前3行。
普通内容(ptr1 == &ticknum
和*ptr1 == ticknum
):
(gdb) p &tickNum
$3 = (uint32_t *) 0x20017fdc
(gdb) p ptr1
$4 = (uint32_t *) 0x20017fdc
(gdb) p *ptr1
$5 = 123
现在执行最后一行代码。
奇怪的东西(ptr2 != &SystemCoreClock
和*ptr2 != SystemCoreClock
):
(gdb) p &SystemCoreClock
$6 = (uint32_t *) 0x20000004 <SystemCoreClock>
(gdb) p ptr2
$7 = (uint32_t *) 0x681b4b20 // should be 0x20000004, beyond end of memory (see memory map bellow)
(gdb) p SystemCoreClock
$8 = 4000000
(gdb) p *ptr2
$9 = 0 // should be 4000000
(gdb) p tickNum
$10 = 0 // should be 4000000
怎么可能?!
内存映射,我对链接器脚本的猜测:
0x20000000 - 0x20018000, LENGTH = 96K RAM
0x10000000 - 0x10008000, LENGTH = 32K RAM
0x08000000 - 0x08100000, LENGTH = 1024K FLASH
真正的链接描述文件:
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K
RAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K
}