01、前言
不說廢話,先上示例代碼
uint8_t num_byte[4]; uint32_t num_word; const uint32_t num_word_const = 0x1234; uint32_t *point_heap; int main(void) { uint8_t num_byte_stack; static uint8_t num_byte_static; point_heap = (uint32_t *)malloc(4); *point_heap = 0x3421; free(point_heap); num_byte_stack = 0x11; #pragma section = "CSTACK" char *pbeginstk = __section_begin("CSTACK"); #pragma section = "HEAP" char *pbeginheap = __section_begin("HEAP"); printf("CSTACK addr is 0x%x\r\n",pbeginstk); printf("HEAP addr is 0x%x\r\n",pbeginheap); printf("num_byte addr is 0x%x\r\n",&num_byte); printf("num_word addr is 0x%x\r\n",&num_word); printf("num_word_const addr is 0x%x\r\n",&num_word_const); printf("point_heap addr is 0x%x\r\n",&point_heap); printf("point_heap is 0x%x\r\n",point_heap); printf("num_byte_stack addr is 0x%x\r\n",&num_byte_stack); printf("num_byte_static addr is 0x%x\r\n",&num_byte_static); }
列印如下
STACK addr is 0x20000320 HEAP addr is 0x20000720 num_byte addr is 0x20000308 num_word addr is 0x2000030c num_word_const addr is 0x8002a44 point_heap addr is 0x20000310 point_heap is 0x20000728 num_byte_stack addr is 0x200006f8 num_byte_static addr is 0x20000318
先說結論:
num_byte、num_word、num_byte_static和point_heap存盤在內部RAM中,
num_byte_stack存貯在堆疊中,
point_heap申請到的記憶體在堆中,
num_word_const在內部flash中,
如果是有同學對這個了然于胸,可以出門左轉了,如果有些同學有興趣,可以進一步往下看,
02、大小端
因為后面的內容涉及到大小端問題,這里先說下大小端問題,
大端(Big-endian):資料的高位位元組存放在地址的低端低位位元組存放在地址高端;
小端(Little-endian):資料的高位位元組存放在地址的高端低位位元組存放在地址低端;
例如:
資料0x12345678存盤格式
大端格式
低地址<----0x12|0x34|0x56|0x78---->高地址
小端格式
低地址<----0x78|0x56|0x34|0x12---->高地址

其中的地址,一般由編譯器分配,也可在程式中自行指定,從上表中,可以清晰的看到,大小端是以位元組為單位進行資料儲存的方式,大端通俗的理解就是賦值數從左自右;小端則是從右自左,
我們常用的X86結構是小端模式,而KEILC51則為大端模式,很多的ARM,DSP都為小端模式,本文使用的平臺STM32F207就是小段模式,
03、逐步分析
如果有同學對這部分不是很熟悉,建議先看一下我之前的推文《C語言的記憶體分配》,先把C語言的堆疊,記憶體等概念先熟悉下,
先說關于堆疊的問題,下面代碼可以列印出IAR平臺下STM32的堆疊起始位置,
#pragma section = "CSTACK" char *pbeginstk = __section_begin("CSTACK"); #pragma section = "HEAP" char *pbeginheap = __section_begin("HEAP");
列印的結果如下
STACK addr is 0x20000320 HEAP addr is 0x20000720
這個地址是否正確,我們可以在IARdebug時,使用Disassembly視窗查看,

關于堆疊大小問題,如下

可以查到堆疊的終止位置是0x20000720,堆的終止位置是0x20000920,注意:這里計算牽扯到大小端的問題,
通過計算:
堆疊的大小=0x20000720-0x20000320=0x400,
堆的大小=0x20000920-0x20000720=0x200,
這和我們在IAR中的堆疊配置是一樣的,

接下來就先說一下分配在記憶體的變數,
通過列印看出,num_byte、num_word、num_byte_static和point_heap并不在堆疊中,它們存盤在內部RAM中,
使用Disassembly視窗查看如下

這也驗證了static關鍵字,在修飾函式內的區域變數時,這個變數將和全域變數一樣存盤在內部ram中,
同時也說明了,STM32內部分配記憶體時候,是先分配全域變數(和static修飾的區域變數),再分配堆疊,最后再分配堆的,
對于堆疊的記憶體分配,區域變數,也就是num_byte_stack是存盤在堆疊的范圍內,
num_byte_stack addr is 0x200006f8
它的地址空間在堆疊中,因為在代碼中num_byte_stack =0x11;使用Disassembly視窗查看到對應的地址數值是0x11,

關于堆疊,再說一句,堆疊不僅僅保存了區域變數,它會在函式切換,中斷發生時保存現場,保存ARM內核的暫存器,這些不是這篇文章的討論重點,這里先挖個坑,等以后有空再寫篇文章專門說說這個部分,
堆的問題,簡單來說:malloc申請的記憶體都在堆中,point_heap指標指向的記憶體地址就在堆的范圍內,
point_heap is 0x20000728
代碼中*point_heap= 0x3421;在Disassembly視窗查看到對應的地址數值是0x3421,

最后一個num_word_const,const修飾的變數是存盤在內部flash中的,它的地址在內部flash范圍內,
在代碼中也有對應的賦值操作,constuint32_t num_word_const = 0x1234;在Disassembly視窗查看到對應的地址數值是0x1234,

點擊查看本文所在的專輯,STM32F207教程
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/390201.html
標籤:其他
上一篇:Linux三劍客命令—awk
