当前位置:网站首页>Initialization of global and static variables

Initialization of global and static variables

2022-07-05 06:50:00 An embedded enthusiast

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

原网站

版权声明
本文为[An embedded enthusiast]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050636022602.html