当前位置:网站首页>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
边栏推荐
- To: Apple CEO Cook: great ideas come from constantly rejecting the status quo
- 9. Intelligent transportation project (2)
- 最小生成树、最短路径、拓扑排序、关键路径
- The successfully resolved idea cannot use the log normally after referencing Lombok's @slf4j
- MySQL recharge
- NFTGameFi链游系统开发详解方案丨链游系统开发原理解析
- Wechat applet custom pop-up components
- Xlua get button registration click event of ugui
- Deep learning: numpy
- Project practice 5: build elk log collection system
猜你喜欢

50 lines of code to crawl TOP500 books and import TXT documents

Pinda general permission system (day 1~day 2)

Tiktok practice ~ sharing module ~ generate short video QR code

数据库SQL语句撰写

Deep learning: numpy

商品秒杀系统

wm_ Concat() and group_ Concat() function

Wechat applet custom pop-up components

Redis单点登陆系统+投票系统

Installation and use of filebeat
随机推荐
Successfully solved the problem of garbled microservice @value obtaining configuration file
链游开发成品源码 链游系统开发详情说明
为什么我不推荐去SAP培训机构参加培训?
Project practice 6: distributed transaction Seata
微信小程序 自定义 弹框组件
[kubernetes] kubernetes principle analysis and practical application (under update)
Several delete operations in SQL
Reading notes: process consulting III
Web resource preloading - production environment practice
物联网协议的王者:MQTT
Nftgamefi chain game system development detailed solution - chain game system development principle analysis
Installation and use of filebeat
Clion compiling catkin_ WS (short for ROS workspace package) loads cmakelists Txt problems
Example of using QPushButton style (and method of adding drop-down menu to button SetMenu)
Leetcode interview question 29 clockwise print matrix
Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用
mysql的充值问题
Development principle analysis and source code of dapp-lp single and dual currency liquidity pledge mining system
数据库SQL语句撰写
IK分词器