C/C++/Java Static / Const 的概念

这里以C为准,其他语言类似。

Static变量是指分配不变(只可分配一次,以后再分配就无效了。)的变量 -- 它的存活寿命或伸展域可以贯穿程序运行的所有过程。这个概念与“ephemeral-短命的”,分配即变的,变量恰恰相反。常常被人们称作的局部变量就是分配即变的。分配即变的变量的存储空间的分配或者回收都是在Stack上完成。相反,分配不变变量的存储空间实在heap memory上动态分配的。

当一个程序被加载到内存,Static变量被存放在程序的地址空间的数据区(如果已初始化了),或者BBS区(BBS Segment)(如果没有被初始化)。 并且在加载之前就被存放在对应的对象文件的区域。

Static 关键字在 C 语言和其他相关语言中都是指这个静态分配的意思或类似意思。



--- 作用域 ---

Scope[edit]

See also: Variable (computer science)#Scope and extent

In terms of scope and extent, static variables have extent the entire run of the program, but may have more limited scope. A basic distinction is between a static global variable, which is in scope throughout the program, and a static local variable, which
is only in scope within a function (or other local scope). A static variable may also have module scope or some variant, such as internal linkage in C, which is a form of file scope or module scope.

In object-oriented programming there is also the concept of a static member variable, which is a "class variable" of a statically defined class – a member variable of a given class which is shared across all instances (objects), and is accessible as a member
variable of these objects. Note however that a class variable of a dynamically defined class, in languages where classes can be defined at run time, is allocated when the class is defined and is not static.

 --- 程序示例 Example ---

An example of static local variable in C:

#include <stdio.h>

void func() {
static int x = 0; // x is initialized only once across three calls of func()
printf("%d\n", x); // outputs the value of x
x = x + 1;
} int main(int argc, char *argv[]) {
func(); // prints 0
func(); // prints 1
func(); // prints 2
return 0;
}

Static / Const 的概念的更多相关文章

  1. (转)全局变量、extern/static/const区别与联系

    全局变量.extern/static/const区别与联系 编译单元(模块):     在IDE开发工具大行其道的今天,对于编译的一些概念很多人已经不再清楚了,很多程序员最怕的就是处理连接错误(LIN ...

  2. static、const和static const

    http://blog.csdn.net/rainkin1993/article/details/8068558 #include<iostream> using namespace st ...

  3. static 类成员变量 和 static const类成员变量

    1.使用static类的优点: (1)避免与其他类的成员或者全局变量冲突 (2)可以封装 (3)阅读性好 2.static 数据成员独立于该类的任意对象而存在 static数据成员的类型可以是该成员所 ...

  4. static const vs. extern const

    在实现文件(.m文件)中使用static const来定义“只在编译单元内可见的常量”(只在.m文件内可见),由于此类常量不在全局符号表中,所以无须为其名称加类名前缀(一般以k开头). 在头文件中使用 ...

  5. iOS—— static和const联合使用;使用static const 与 #define

    static和const联合使用:   static将一个全局变量变成局部变量   const将一个局部变量变成局部常量 // 定义了一个局部常量      static const CGFloat ...

  6. Static Const

    Static 内部的 Const 不可变的 一般写法 在.m文件中, static NSString *const ID = @"shop"; static const CGFlo ...

  7. (转) C++ static、const和static const 以及它们的初始化

    const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. static表示的是静态的.类的静态成员函数.静态成员变量是和类相关的,而不是和类的 ...

  8. C++ static、const和static const 以及它们的初始化

    转自C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. s ...

  9. 类内const static(static const)成员变量初始化问题

    在查找const相关资料的过程中,又遇到了另外一个问题,就是C++类中const static(或者static const)成员变量应当如何初始化的问题. 查阅了许多资料,发现VC环境下,只允许co ...

随机推荐

  1. Spring Boot启动 Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator错误

    开始运行得很好的项目,因为前一天高度了项目结构和名称突然报上面的错误 查了很多网上资料很多解决方案 造成这个错误的原因有很多,例如 1.@Entity 类有变动,无非正常生成对应的数据库. 解决:使用 ...

  2. STM32 用c语言控制4个LED灯从左向右无限流动

    在用c语言写LED流水灯的前提条件是配置好其他环境,这里我就不说环境了, 想让LED灯无限循环时,首先要想到的是无限循环函数,我这里利用的是for函数 无限循环. #include "stm ...

  3. Unity 3D编辑器扩展介绍、教程(一) —— 创建菜单项

    Unity编辑器扩展教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 一 Brief Introduct ...

  4. CF1119 Global Round 2

    CF1119A Ilya and a Colorful Walk 这题二分是假的.. \(1,2,1,2,1\) 有间隔为 \(3\) 的,但没有间隔为 \(2\) 的.开始被 \(hack\) 了一 ...

  5. Windows环境下搭建Nginx和多版本PHP共存

    PHP版本众多,彼此之间兼容性不好,不同程序经常需要的不同版本的PHP版本.这里介绍如何使用NGINX让不同版本的PHP共存. 软件下载地址 与nginx整合的php需要选择Non Thread Sa ...

  6. HDU5373 The shortest problem (YY)

    http://acm.hdu.edu.cn/showproblem.php?pid=5373 YY题,模拟下计算过程就好了,计算中并不要保存实际数(这个数会非常大),只要保存到目前为止的数字位上的和 ...

  7. Open-sourcing sso, the way we secure services at BuzzFeed

    文章来源: https://tech.buzzfeed.com/unleashing-the-a6a1a5da39d6 说明: 设计有好多地方值得借鉴,粘贴过来的排版不好 Today we are o ...

  8. ory Oathkeeper cloud native 访问认证平台

    ORY Oathkeeper is an Identity & Access Proxy (IAP) that authorizes HTTP requests based on sets o ...

  9. git 报错及解决

    报错:fatal: refusing to merge unrelated histories==== 解决办法:git pull加上参数,如:git pull –allow-unrelated-hi ...

  10. 服务检测sh脚本

    如mysql cat check_mysql.sh #!/bin/bash servicename="mysqld"showname="mysql" pid=& ...