当前位置:网站首页>Initialization of global and static variables
Initialization of global and static variables
2022-06-29 20:21:00 【An embedded enthusiast】
Initialization of global and static variables
Global variables 、static Variable initialization time
Static local variables
First , Static local variables are the same as global variables , The data is stored in the global area , So before the main program , The compiler has allocated memory for it , But in C and C++ The initialization node of static local variables in is a little different .
stay C in , Initialization occurs before code execution , After allocating memory in the compilation phase , It will initialize , So we see stay C Variables cannot be used to initialize static local variables in language , At the end of the program , The global memory where the variable is located will be fully recycled .
And in the C++ in , When initializing, it will be initialized when the relevant code is executed , Mainly due to C++ After introducing objects , To initialize, the corresponding constructor and destructor must be executed , In the constructor or destructor, you often need to do some specific operations in the program , It's not just about allocating memory . therefore C++ The standard is that global or static objects are constructed only when they are first used , And pass atexit() To manage . At the end of the program , According to the order of construction, they are destructed one by one in the opposite direction . therefore stay C++ Variables can be used to initialize static local variables Of .
Global variables 、 The static variables of the file field and the static member variables of the class are in main Allocate memory and initialize during previous static initialization ; Local static variable ( Generally, it is a static variable in a function ) Allocate memory and initialize on first use . The variables here contain objects of built-in data types and custom types .
Global variables
according to C++ standard , The initialization of global variables should be in main Complete before function execution , Common sense is beyond doubt , But this statement is a little vague ,main When exactly is it before the function is executed ? Is it compile time or run time ? The answer is both compile time , There may also be runtime (seriously), From the language level , The initialization of global variables can be divided into the following two stages (c++11 N3690 3.6.2):
Don't write programs related to compilation order
For global variables of different compilation units , The order of initialization is not guaranteed , Therefore, for global variables in different compilation units , It is unwise to establish dependencies between their initialization sequences . In addition, there is no way to catch the exception thrown by global variable initialization , Generally speaking, reduce the use of global variables , In particular, limit global variables that require complex initialization .so:
1、 Try not to use global variables
2、 Use static variables , Access through accessors
for example : Global variables
int a = 5;
int b = a;
If a, and b Defined in the same file , That's no problem , result b be equal to 5. If a and b Defined in different documents , There is no guarantee that b Also equal to 5, That is to say, there is no guarantee that a Initialize first . in fact , Except that the initialization of global objects defined in the same file is carried out in the defined order , There is no guarantee of the initialization order between other global or static variables . The solution to this problem is not to use global variables directly , Instead, use a wrapper function to access , for example
int get_a()
{
static int a = 5;
return a;
}
int get_b()
{
static int b = get_a();
return b;
}
In this case , No matter what get_a and get_b Is it defined in the same file ,get_b Always return the right results , The reason lies in , Static variables inside a function are initialized the first time they are accessed .
anytime , If... Is defined in different compiled units " Nonlocal static objects ", And the correct behavior of these objects depends on a particular order in which they are initialized , There will be problems . You can never control the initialization order of nonlocal static objects in different compiled units . For static objects in functions ( namely " Local " Static objects ) They are initialized when they first encounter the definition of an object during a function call …
summary
1、 Static variables are initialized at compile time , The assignment of variables is carried out when the function or program is running .
2、 Static variables are initialized only once , However, the value of a static variable can be modified many times by assignment .
3、 Global and static variables When entering main Initialized before
边栏推荐
- Jupyter service installation and startup
- Mysql Json 数据类型&函数
- Tag based augmented reality using OpenCV
- Golang basic learning
- freemarker模板框架生成图片
- [compilation principle] syntax analysis
- How to use filters in jfinal to monitor Druid for SQL execution?
- A Japanese Cherry sold at a sky high price of 1980 yuan. Netizen: I feel cheated after eating it
- 关于印发宝安区重点产业项目和总部项目遴选及用地保障实施细则(2022修订版)的通知
- PHP implementation extracts non repeated integers (programming topics can be the fastest familiar functions)
猜你喜欢

Cmake development - Multi Directory Project

Comparable比较器写法&ClassCastExcption类转换异常

4-1 port scanning technology

【编译原理】类型检查

Configuration du Flume 4 - source personnalisée + sink

Coreldraw2022 new version v24.1.0.360 update

Fastadmin background setting radio button

Introduction to the latest version 24.1.0.360 update of CorelDRAW

Bigder: Automation Test Engineer

Flume配置1——基础案例
随机推荐
proxmox集群节点崩溃处理
2021 CCPC Harbin E. power and modulo (thinking questions)
命令执行(RCE)漏洞
0/1 score planning topic
「运维有小邓」Active Directory 密码过期通知功能
[compilation principle] semantic analysis
freemarker模板框架生成图片
雪花id,分布式唯一id
Codeforces Global Round 21 C D E
【编译原理】语法分析
2021 CCPC 哈尔滨 J. Local Minimum (思维题)
Flutter calls Baidu map app to realize location search and route planning
网站压力测试工具——Webbench
. NETCORE unified authentication authorization learning - first authorization (2)
Bigder:自动化测试工程师
XSS vulnerability
How to use filters in jfinal to monitor Druid for SQL execution?
.NetCore统一认证授权学习——Run(1)
使用Gunicorn部署web.py应用
Analysis of the underlying architecture of spark storage system - spark business environment practice