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. [LeetCode&Python] Problem 728. Self Dividing Numbers

    A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...

  2. JAVA解压文件

    package com.chauvet.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...

  3. 【HAOI2013】花卉节

    HA果然是弱省中的弱省…… 原题: ZZ市准备在绿博园举办一次花卉节.Dr.Kong接受到一个任务,要买一批花卉进行布置园林.能投入买花卉的资金只有B元 (1 <= B <= 10^18) ...

  4. css中display为none 和visibility为hidden的区别

    区别一: display:none Turns off the display of an element (it has no effect on layout);  all child eleme ...

  5. 推荐10款优秀的JavaScript Web UI库 框架和套件

    在进行Web开发时,并非所有的库都适合你的项目,但真正开发的时候,你任然需要依赖一款UI框架.特别在你时间紧迫的时候,它是你忠实的朋友. 他们都是些广泛使用包含不同语言实现的WEB UI框架.今天我就 ...

  6. 小米盒子root及sshdroid安装

    1.root 参考屌丝猫的教程 主要原理是通过运行自定义recovery实现root功能 2.安装sshdroid以及幸运破解器 3.使用幸运破解器吧sshdroid编程系统应用,从而实现自启动

  7. Linux开机自动启动ORACLE设置

    1.安装好Oracle数据库后: 执行 dbstart和dbshut会提示: [oracle@oracle11g ~]$ dbstartORACLE_HOME_LISTNER is not SET, ...

  8. POJ2584 T-Shirt Gumbo——网络最大流模板

    题目:http://poj.org/problem?id=2584 像模板一样的简单题.继续使用 & 的当前弧优化和神奇的构造函数. #include<iostream> #inc ...

  9. Kindle一周使用感受

    为何选择Kindle 「Kindle」终于入手,心情十分愉悦^_^,入手的是499块「Kindle国行版」,个人感觉电子墨水屏显示效果很赞,很适合在光线比较充足的环境下阅读,即使在中午的阳光底下使用K ...

  10. Tomcat 去除项目名称

    再tomcat的conf下server.xml  里, 再<host>...</host>的标签之间添加 <Context path="" docBa ...