当前位置:网站首页>When are global variables initialized before entering the main function?
When are global variables initialized before entering the main function?
2022-06-26 19:24:00 【Endless learning 2022】
Global variables ,static Variable initialization time
First , Static local variables are the same as global variables , The data is stored in the global area Memory distribution , 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 in 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 . So in C++ You can use variables to initialize static local variables .
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 .
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):
- Static variables are initialized at compile time , The assignment of variables is carried out when the function or program is running .
- Static variables are initialized only once , However, the value of a static variable can be modified many times by assignment .
- Global and static variables When entering main Initialized before
边栏推荐
- [recommended collection] these 8 common missing value filling skills must be mastered
- 问题解决:虚拟机无法复制粘贴文件
- Button how to dynamically set drawablebottom (setcomposunddrawables is invalid)
- 两个文件 合并为第三个文件 。
- Deep learning: numpy
- Tiktok practice ~ sharing module ~ generate short video QR code
- Wechat applet uniapp left slide delete with Delete Icon
- String string is converted to jsonarray and parsed
- Request method 'POST' not supported
- Tiktok practice ~ sharing module ~ copy short video link
猜你喜欢
随机推荐
PC end records 515 ground sweeping robot /scan data
C语言 文件光标 fseek
Filebeat安装及使用
MySQL recharge
Publish message publishers and subscribe message subscribers of ROS
为什么我不推荐去SAP培训机构参加培训?
Commodity seckill system
Invocation failed Unexpected end of file from server
Convex hull problem
【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)
Tree array
关于不等式取值转义的思路
Clion compiling catkin_ WS (short for ROS workspace package) loads cmakelists Txt problems
Tiktok practice ~ sharing module ~ generate short video QR code
手机影像内卷几时休?
自己创建一个时间拦截器
商品秒杀系统
Six necessary threat tracking tools for threat hunters
問題解决:虛擬機無法複制粘貼文件
Using recursion to find all gray codes with n bits









