当前位置:网站首页>全局变量&局部变量
全局变量&局部变量
2022-06-25 06:38:00 【芯辰大海】
作用域:
全局变量:其作用范围是“整个工程”,只需在一个源文件中定义,就可以作用于所有的源文件。当然,其他不包含全局变量定义的源文件需要用extern 关键字再次声明这个全局变量
静态全局变量:使用 static 关键字修饰,也具有全局作用功能,和全局变量区别在于如果该程序包含多个文件,其作用范围仅在定义的那个文件,不能作用于其它文件,这样即使两个不同的源文件都定义了相同名字的静态全局变量,它们也是不同的变量
局部变量:仅仅从定义的位置开始,到定义它的右花括号结束,只在函数执行期间存在,函数的一次调用执行结束后,变量被撤销,其所占用的内存也被收回
静态局部变量:局部作用域,它只被初始化一次,自从第一次被初始化直到程序运行结束都一直存在,和局部变量的区别在于函数执行完也还存在
生存期:
全局变量:随进程持续性
静态全局变量:随进程持续性
局部变量:从定义开始到函数结束,函数调用后变量就被撤销,内存被回收
静态局部变量:随进程持续性,static 修饰的局部变量其生存期从函数变为整个进程
内存分配:
全局变量:全局(静态)存储区
静态全局变量:全局(静态)存储区
局部变量:存放在栈中,只有在所在函数被调用时才动态地为变量分配存储单元
静态局部变量:全局(静态)存储区
其它:
全局(静态)存储区:分为DATA段和BSS段。DATA段(全局初始化区)存放初始化的全局变量和静态变量;BSS段(全局未初始化区)存放未初始化的全局变量和静态变量。程序运行结束时自动释放。其中BBS段在程序执行之前会被系统自动清0,所以未初始化的全局变量和静态变量在程序执行之前已经为0
静态变量会被放在程序的静态数据存储区(全局可见)中,这样可以在下一次调用的时候还可以保持原来的赋值。这一点是它与堆栈变量和堆变量的区别
变量用static告知编译器,自己仅仅在变量的作用范围内可见。这一点是它与全局变量的区别
把局部变量改变为静态变量后是改变了它的存储方式即改变了它的生存期。把全局变量改变为静态变量后是改变了它的作用域,限制了它的使用范围。因此static 这个说明符在不同的地方所起的作用是不同的;
边栏推荐
- Cloning and importing DOM nodes
- Changing the background color of tab bar - changing the background color of tab bar
- 有了 MySQL 为什么要用 NoSQL?
- lotus windowPoSt 手动触发时空证明计算
- Report on development status and investment strategy recommendations of global and Chinese graphite polystyrene board industry 2022-2028
- 赚够钱回老家吗
- [C language] add separator to string
- 太美的承诺因为太年轻
- R & D thinking 07 - embedded intelligent product safety certification required
- 【一起上水硕系列】Day 4
猜你喜欢

The process of making wooden barrels with 3DMAX software: a three-step process

Simple and complete steps of vivado project

Practice of hierarchical management based on kubesphere

MCU IO explanation (pull-up pull-down quasi bidirectional input / output push-pull open drain)

有了 MySQL 为什么要用 NoSQL?

How to find happiness in programming and get lasting motivation?

We are different

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

弱大数定理的意义与证明

单片机IO详解(上拉 下拉 准双向 输入 输出 推挽 开漏)
随机推荐
[C language] add separator to string
How do I know if mysqlnd is an active driver- How to know if MySQLnd is the active driver?
Analysis on the trend of the number of national cinemas, film viewers and average ticket prices in 2021 [figure]
alphassl通配符证书送一个月
聚类和分类的最基本区别。
Why is true == "true" true in R- Why TRUE == “TRUE” is TRUE in R?
Atomic alpha development board -- SD card and EMMC burning tool
The upper and lower lines of the shell are merged into one line
[he doesn't mention love, but every word is love]
太美的承诺因为太年轻
破万,我用了六年!
Report on development status and investment strategy recommendations of global and Chinese graphite polystyrene board industry 2022-2028
Escape analysis of 982 golang
CTFHub-Web-信息泄露-目录遍历
Coffee script unmatched outent error
父爱的表达方式
Solar orbiter captured the full picture of the largest prominence eruption in history
Blue Bridge Cup SCM module code (matrix key) (code + comments)
MySQL(十二)——更改表的备注
Uncaught TypeError: Cannot read properties of undefined (reading ‘prototype‘)