当前位置:网站首页>全局变量&局部变量
全局变量&局部变量
2022-06-25 06:38:00 【芯辰大海】
作用域:
全局变量:其作用范围是“整个工程”,只需在一个源文件中定义,就可以作用于所有的源文件。当然,其他不包含全局变量定义的源文件需要用extern 关键字再次声明这个全局变量
静态全局变量:使用 static 关键字修饰,也具有全局作用功能,和全局变量区别在于如果该程序包含多个文件,其作用范围仅在定义的那个文件,不能作用于其它文件,这样即使两个不同的源文件都定义了相同名字的静态全局变量,它们也是不同的变量
局部变量:仅仅从定义的位置开始,到定义它的右花括号结束,只在函数执行期间存在,函数的一次调用执行结束后,变量被撤销,其所占用的内存也被收回
静态局部变量:局部作用域,它只被初始化一次,自从第一次被初始化直到程序运行结束都一直存在,和局部变量的区别在于函数执行完也还存在
生存期:
全局变量:随进程持续性
静态全局变量:随进程持续性
局部变量:从定义开始到函数结束,函数调用后变量就被撤销,内存被回收
静态局部变量:随进程持续性,static 修饰的局部变量其生存期从函数变为整个进程
内存分配:
全局变量:全局(静态)存储区
静态全局变量:全局(静态)存储区
局部变量:存放在栈中,只有在所在函数被调用时才动态地为变量分配存储单元
静态局部变量:全局(静态)存储区
其它:
全局(静态)存储区:分为DATA段和BSS段。DATA段(全局初始化区)存放初始化的全局变量和静态变量;BSS段(全局未初始化区)存放未初始化的全局变量和静态变量。程序运行结束时自动释放。其中BBS段在程序执行之前会被系统自动清0,所以未初始化的全局变量和静态变量在程序执行之前已经为0
静态变量会被放在程序的静态数据存储区(全局可见)中,这样可以在下一次调用的时候还可以保持原来的赋值。这一点是它与堆栈变量和堆变量的区别
变量用static告知编译器,自己仅仅在变量的作用范围内可见。这一点是它与全局变量的区别
把局部变量改变为静态变量后是改变了它的存储方式即改变了它的生存期。把全局变量改变为静态变量后是改变了它的作用域,限制了它的使用范围。因此static 这个说明符在不同的地方所起的作用是不同的;
边栏推荐
- 1W字|40 图|硬核 ES 实战
- 【C语言】给字符串增加分隔符
- 有了 MySQL 为什么要用 NoSQL?
- Jameswebb Space Telescope goes into operation to help study interstellar objects
- joda. Time get date summary
- Lotus windowsost manually triggers space-time proof calculation
- R & D thinking 07 - embedded intelligent product safety certification required
- 3dmax软件的制作木桶过程:三步流程
- Change the current count of auto increment values in MySQL- Changing the current count of an Auto Increment value in MySQL?
- In depth analysis of Apache bookkeeper series: Part 3 - reading principle
猜你喜欢

深入解析 Apache BookKeeper 系列:第三篇——读取原理

Error reported during vivado simulation common 17-39
![[Yu Yue education] engineering testing technology reference of Wenhua University](/img/69/50a8786ea062a541df9e07c1e16db5.jpg)
[Yu Yue education] engineering testing technology reference of Wenhua University

What is the new business model of Taishan crowdfunding in 2022?

Want to self-study SCM, do you have any books and boards worth recommending?

活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭

2022 biological fermentation Exhibition (Jinan), which is a must read before the exhibition. The most comprehensive exhibition strategy will take you around the "fermentation circle"

In depth analysis of Apache bookkeeper series: Part 3 - reading principle

父爱的表达方式

Pratique de gestion hiérarchique basée sur kubesphere
随机推荐
Shandong finds clean energy that can be used by China for 3800 years? You should know the truth first
レ / leilei
In depth analysis of Apache bookkeeper series: Part 3 - reading principle
Why did Yanjing Beer come here?
lotus windowPoSt 手动触发时空证明计算
Analysis on the output, market scale and development status of China's children's furniture industry in 2020 and the competition pattern of children's furniture enterprises [figure]
网络是怎样连接的?
I have used it for six years!
【工具分享】一款颜值与技能并重的软件
【2022黑马程序员】SQL优化
Escape analysis of 982 golang
[tool sharing] a software that pays equal attention to appearance and skills
The upper and lower lines of the shell are merged into one line
直接选择排序和快速排序
[learn FPGA programming from scratch -43]: vision chapter - technology evolution of chip design in the post Moore era -2- evolution direction
Wow, it's so rich.
Message queue table structure for storing message data
单片机IO详解(上拉 下拉 准双向 输入 输出 推挽 开漏)
The most basic difference between clustering and classification.
ES can finally find brother Wukong!