当前位置:网站首页>Virtual memory location structure (reserved area, code area, stack area, heap area, literal constant area) and variable modifiers (const, auto, static, register, volatile, extern)
Virtual memory location structure (reserved area, code area, stack area, heap area, literal constant area) and variable modifiers (const, auto, static, register, volatile, extern)
2022-07-16 06:35:00 【Sometimes crazy options】
Virtual memory location structure and variable modifiers (const、auto、static、register、volatile、extern)
List of articles
32 Bit operating system virtual memory

With 32 For example, bit operating system
Reserves
Code section
Literal constant area ( Do not modify the )
- char *str = “XXXX”
- Global area const
Compiled code instructions
Global data area
Stack area
The stack area
- Local area
- Function arguments
Stack buffer
Heap area :
- malloc/calloc Application and release
Kernel area
64 Bit operating system virtual memory structure

#include<stdio.h>
#include<string.h>
#define PTD(a,b,c) a##_##b##_##c
#define PRINTp(a,b,c) printf(#a"_"#b"_"#c" = %p\n",(&(PTD(a,b,c))))
#define PRINT(a,b,c) printf(#a"_"#b"_"#c" = %p\n",(PTD(a,b,c)))
int PTD(g,i,d)=1;//data paragraph
int PTD(g,i,u); //bss paragraph
char PTD(g,c,u); //bss paragraph
char PTD(g,c,d) = 'b'; //data paragraph
char PTD(g,s,u)[5]; //bss paragraph
char PTD(g,s,d)[5] = "asdf"; //data paragraph
char *PTD(g,pc,u); // Literal constant area
char *PTD(g,pc,d) = "sad"; // Literal constant area
const int PTD(gc,i,d) = 1; // Literal constant area
const int PTD(gc,i,u);
int func(){
static int a;// Global area bss paragraph
static int b;// Global area data paragraph
printf("static &a = %p\n",&a);
printf("static &b = %p\n",&b);
return 0;
}
int main(){
int PTD(m,i,d)=2;// Stack
int PTD(m,i,u);// Stack
const int PTD(mc,i,d)=1;// Stack
const int PTD(mc,i,u);// Stack
char PTD(m,c,u);// Stack
char PTD(m,c,d) = 'c';// Stack
char PTD(m,s,u)[5];// Stack
char PTD(m,s,d)[5] = "adf";// Stack
char *PTD(m,pc,u);// Literal constant area
char *PTD(m,pc,d) = "swd";// Literal constant area
PRINTp(g,i,d);
PRINTp(g,i,u);
PRINTp(gc,i,d);
PRINTp(gc,i,u);
PRINTp(g,c,d);
PRINTp(g,c,u);
PRINT(g,s,d);
PRINT(g,s,u);
PRINT(g,pc,d);
PRINT(g,pc,u);
PRINTp(m,i,d);
PRINTp(m,i,u);
PRINTp(m,c,d);
PRINTp(m,c,u);
PRINT(m,s,d);
PRINT(m,s,u);
PRINT(m,pc,d);
PRINT(m,pc,u);
func();
return 0;
}
result: g->global,gc->global const, m-> main Function , i->int ,c -> char, s->str ,pc -> Character pointer , d->define, u->undefine
g_i_d = 0x55c7e3221010
g_i_u = 0x55c7e3221054
gc_i_d = 0x55c7e30209a8
gc_i_u = 0x55c7e3221050
g_c_d = 0x55c7e3221014
g_c_u = 0x55c7e3221038
g_s_d = 0x55c7e3221015
g_s_u = 0x55c7e3221048
g_pc_d = 0x55c7e30209a4
g_pc_u = (nil)
m_i_d = 0x7ffc592a545c
m_i_u = 0x7ffc592a5460
m_c_d = 0x7ffc592a545b
m_c_u = 0x7ffc592a545a
m_s_d = 0x7ffc592a5483
m_s_u = 0x7ffc592a547e
m_pc_d = 0x55c7e30209cc
m_pc_u = 0x55c7e3020920
static &a = 0x55c7e322102c
static &b = 0x55c7e3221030
Modifier for variable
const
const effect
- const int num = 10; // Express num Variable read only
- If num It's a local variable Will not change its storage area
- If num Global variable From global data area to literal constant area
- Modify pointer parameters , Prevent accidental modification
- Increase the robustness and readability of the code Convenient maintenance
const With the pointer
const char *p1; //const modification *p1 Indicates that the content pointed to by the pointer cannot be changed *p1 read-only p1 You can change
char const *p2; // ditto const modification *p2 *p2 read-only p2 You can modify
char * const p3; //const modification p3 p3( The pointer itself ) read-only *p3 You can modify
const char * const p4; //*p4 and p4 All read-only
char const * const p5; //*p5 and p5 All read-only
constant pointer And Constant pointer
- constant pointer The essence is constant Pointer to a constant const char *p;
- Constant pointer The essence is the pointer The pointer is a constant char * const p;
auto
- Automatic variable
- The default common variable defined is auto
- [auto] int x = 10;
static
- Static
Static local variables
- Ordinary local variables are stored in the stack ,static Decorated local variables are stored in the global area
- The life cycle of ordinary local variables is during function calls ,static The decorated local variable life cycle is the whole program running period
- Common local variables and static Modified local variables Scope It's the same ( This function starts from the definition )
- The definition of ordinary local variables will be executed once every call ,static Variable definition statements will not be executed repeatedly
Static global variables
- The scope of common global variables is all files of the whole project ,static Modified global variables can only be used in this file
- The storage domain and lifecycle are the same
Static functions / Inline function
- static inline void func(void){}
- When calling an inline function , Call instructions will not be generated , Instead, the binary code instruction of the function is used to directly replace the calling instruction of the function
- A preference for function calls , Improve operational efficiency
- Generally, simple functions that are frequently called can be declared inline
- Recursive functions cannot be inline functions
- C89 The standard does not have inline functions ,C99 Standards were introduced Come in
register
- Register variables
- register Apply for a variable as a register variable , however register Just asking , Whether this variable can be used as a register variable depends on the compiler and processor
- Cannot take... For register variables & ( Stored on the register , Not in virtual memory )
- For some frequently used variables , As a register variable, improve the efficiency
- register It is also required for type , To meet the number of bits of the register ( Machine word length )
volatile
- Changeable
- Used in multi-threaded race , Unexpected changes may occur ,
- The processor is required to reload the variable every time it processes it
- Even if this thread has not been modified , But the values loaded many times may be different
extern
- External
- Only for declaration Declare that a variable has been defined in other files , Make only declarations in the current document
- You can distinguish between global variables and local variables with the same name
- If the local variable has the same name as the global variable , Form a hidden , Local variables with the same name hide global variables with the same name
- Partial priority principle Local variables are directly accessed
int g = 1024;
int main(){
int g = 9527;
printf("g = %d\n",g);// Local g
g = 11111; // Local Partial priority principle
{
extern int g;// Declare external variables
printf("g = %d\n",g);// Overall
g = 2222; // Overall
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
How to make electronic signature transparent
DHT11和DHT22(AM2302)比较及使用方法
notepad++打开bin文件
##DHCP-MASTER自动化部署
[Multisim] problems and solutions of Multisim Simulation "zero crossing comparator"
Keil5快捷键
supervisor系列:5、日志
【CVPR2022】MPViT : Multi-Path Vision Transformer for Dense Prediction
FreeRTOS 学习(一)
RTtread-动态内存分配
DCGAN:DEEP CONVOLUTIONAL GENERATIVE ADVERSARIAL NETWORKS——论文分析
Keil5 shortcut key
Embedded software development stm32f407 buzzer standard library version
HDU 2874 Connections between cities (并查集+lca倍增法)
自动化仪表与过程控制(期末复习)
第一章 DHT11温湿度传感器的使用
【Multisim】关于Multisim仿真“过零比较器”出现的问题以及解决方法
整理numpy
在中断程序中,需要清中断标志,作用是什么?
POJ 2763 Housewife Wind (树链剖分+边权化点权)









