美文网首页
Keil MDK编译器内存分配

Keil MDK编译器内存分配

作者: 土DOU吹雪 | 来源:发表于2019-10-11 16:32 被阅读0次

文章内容大部分摘录自:
Keil MDK编译器内存分配

Code区:即代码区。该区域除了存放指令外,还包括指令数据(inc .data),如局部变量数组的初始化值。当函数被调用时,该函数用code区内指令数据来初始化堆栈区内分配的局部变量数组。注:局部变量用立即数来赋值初始化。

RO区:Read Only data,即只读数据域,它指程序中用到的只读数据,这些数据被存储在ROM区,因而程序不能修改其内容。在MDK中,const修饰的全局数组变量放在这个区域里面,该区域数据是绝对不可变的 ,指向只读变量的指针能够访问该区域,但是不能改变该区域数据。

RW区:Read Write data,即可读写数据域,它指初始化为“非0值”的可读写数据,程序刚运行时,这些数据具有非0的初始值,且运行的时候它们会常驻在RAM 区,因而应用程序可以修改其内容。例如C语言中使用定义的全局变量和静态变量,且定义时赋予“非0值”给该变量进行初始化。

局部变量:

void fun(void)
{
    const unsigned char  str[] = "12345678";          //str在栈中,初始值即为Code区的指令数据
    static  const unsigned char  str[] = "12345678";  //str在RO-data区
    static  unsigned char  str[] = "12345678";        //str在RW-data区
}

全部变量:

const unsigned char  str[] = "12345678";          //str在RO-data区
static  const unsigned char  str[] = "12345678";  //str在RO-data区
static  unsigned char  str[] = "12345678";        //str在RW-data区

局部变量:

void fun(void)
{
    unsigned char *p ="12345678";          //p在栈中,初始值即为Code区的指令数据(inc .data)
    static unsigned char *p ="12345678";   //p在RW-data区,初始值在RO-data区
}

全局变量:

unsigned char *p ="12345678";          //p在RW-data区,初始值在RO-data区
static unsigned char *p ="12345678";   //p在RW-data区,初始值在RO-data区

局部变量:

void fun(void)
{
    const unsigned int i = 5;          //i在栈中
    static const unsigned int i = 5;   //i在RO-data区
}

全局变量:

unsigned int i = 5;                //i在RW-data区
const unsigned int i = 5;          //i在RO-data区
static const unsigned int i = 5;   //i在RO-data区

简单的说,const 生命的变量会在 RO 区,字符串指针所指向的字符串也在 RO 区,所以想对字符串的内容进行操作,应当采用「str[] = "12345678";」这样的声明方式。

值得一提的是,声明:

const char* p = "1234567";

读作 p pointer to const char ,是「指向 const char 类型的指针」,用这种把指针读作「pointer to」的方法,能够更好地读懂代码。

相关文章

网友评论

      本文标题:Keil MDK编译器内存分配

      本文链接:https://www.haomeiwen.com/subject/gucxmctx.html