当前位置:网站首页>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
边栏推荐
猜你喜欢

Record of user behavior log in SSO microservice Engineering

Installation and use of filebeat

Minimum spanning tree, shortest path, topology sorting, critical path

Redis single sign on system + voting system

刷新三观的HP-UX系统中的强指针赋值出core问题

uni-app使用canvas绘制二维码

Crawl Douban to read top250 and import it into SqList database (or excel table)

商品秒杀系统

Wechat applet custom pop-up components

Xlua get button registration click event of ugui
随机推荐
Boot的单元测试
Pinda general permission system (day 3~day 4)
威胁猎人必备的六个威胁追踪工具
知識點總結
C语言 文件光标 fseek
手机影像内卷几时休?
抖音实战~搜索页面~视频详情
Tiktok practice ~ search page ~ video details
C# 练习。类列表加记录,显示记录和清空记录
問題解决:虛擬機無法複制粘貼文件
抖音实战~分享模块~复制短视频链接
Minimum spanning tree, shortest path, topology sorting, critical path
Some cold knowledge about QT database development
8VC Venture Cup 2017 - Final Round C. Nikita and stack
To: seek truth from facts
Clion compiling catkin_ WS (short for ROS workspace package) loads cmakelists Txt problems
Boot指标监测
Leetcode 128 longest continuous sequence
The successfully resolved idea cannot use the log normally after referencing Lombok's @slf4j
项目实战四:用户登录及token访问验证(reids+jwt)