当前位置:网站首页>Initialization of global and static variables
Initialization of global and static variables
2022-07-05 06:50: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
边栏推荐
- 扫盲-以太网MII接口类型大全-MII、RMII、SMII、GMII、RGMII、SGMII、XGMII、XAUI、RXAUI
- [Gaode map POI stepping pit] amap Placesearch cannot be used
- 全局变量和静态变量的初始化
- H5 module suspension drag effect
- ROS2——Service服务(九)
- Xavier CPU & GPU high load power consumption test
- ROS2——topic话题(八)
- LSA Type Explanation - lsa-5 (type 5 LSA - autonomous system external LSA) and lsa-4 (type 4 LSA - ASBR summary LSA) explanation
- Using handler in a new thread
- Get class files and attributes by reflection
猜你喜欢
逻辑结构与物理结构
namespace
ROS2——Service服务(九)
Vscode configures the typera editor for MD
Stack acwing 3302 Expression evaluation
Use the Paping tool to detect TCP port connectivity
Skywalking全部
Get class files and attributes by reflection
The “mode“ argument must be integer. Received an instance of Object
2. Addition and management of Oracle data files
随机推荐
ethtool 原理介绍和解决网卡丢包排查思路(附ethtool源码下载)
[nvidia] CUDA_ VISIBLE_ DEVICES
new和malloc的区别
In C language, int a= 'R'
Game theory acwing 894 Split Nim game
*P++, (*p) + +, * (p++) differences
【高德地图POI踩坑】AMap.PlaceSearch无法使用
Volcano 资源预留特性
About vscode, "code unreachable" will be displayed when calling sendline series functions with pwntools“
kata container
Qt项目中的日志库log4qt使用
7. Oracle table structure
2022 winter vacation training game 5
如何正确在CSDN问答进行提问
MySQL (UDF authorization)
扫盲-以太网MII接口类型大全-MII、RMII、SMII、GMII、RGMII、SGMII、XGMII、XAUI、RXAUI
What is linting
SOC_SD_DATA_FSM
[Gaode map POI stepping pit] amap Placesearch cannot be used
Package webapp or H5 pages into apps