关于静态变量的的介绍

In computer programming, a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program. This is in contrast to the more ephemeral automatic variables (local variables are generally automatic), whose storage is allocated and deallocated on the call stack; and in contrast to objects whose storage is dynamically allocated in heap memory.

When a program (executable or library) is loaded into memory, static variables are stored in the data segment of the program's address space (if initialized), or the BSS segment (if uninitialized), and are stored in corresponding sections of object files prior to loading.

The static keyword is used in C and related languages both for static variables and other concepts.


 A basic distinction is between a static global variable, which has global scope and thus is in context throughout the program, and a static local variable, which has local scope and thus is only in context within a function (or other local context). 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.


全局变量和全部静态变量,全局静态变量和局部静态变量


Globalvariables are variables defined outside of any function. Their scope starts atthe point where they are defined and lasts to the end of the file. They haveexternal linkage, which means that in other sourcefiles, the same name refers to the same location in memory.
Static global variables are private to the source file where they are definedand do not conflict with other variables in other source files which would havethe same name.

 

Asa sidenote, both global and static variables have static initialization, whichmeans that if you don't set them to a value, they will be initialized to 0(common variables) or NULL(pointers). This is the only case in ANSI C where you can assume that the valueis zero without initializing it explicitly.

 

Static global variables: variables declared as static at the toplevel of a source file (outside any function definitions) are only visiblethroughout that file ("file scope",also known as "internal linkage").

 

Static local variables: variablesdeclared as static inside a function are statically allocated, thus keeptheir memory cell thoughout all program execution, while having the same scopeof visibility as automatic local variables,meaning remain local to the function. Hence whatever values the function putsinto its static local variables during one call will still be present when thefunction is called again.

 

 

 

 

 

The meaning of the"static" keyword in C depends on whether or not it appears within abrace-delimited block. Refer to the following code snippet.

1.      int a;

2.      static int b;

3.       

4.      int func(void) {

5.      int c;

6.      static int d;

7.      ...

8.      }

Variables 'a' and 'b' are declared outside of any brace-delimitedblocks, therefore they both persist for the life of the program and areaccessible from anywhere in the compilation unit. In this case, the"static" keyword has the effect of making variable 'b' local to thecompilation unit. Variable 'a' can be accessed from any other compilation unitsthat contain an extern declaration for it.

Variables 'c' and 'd' are declared within a brace-delimited block, thereforethey are only accessible from within that block. In this case, the"static" keyword has the effect of making variable 'd' persist forthe life of the program. Variable 'c' only persists while execution is withinthat brace-delimited block. There is no reason to expect variable 'c' to bestored at the same location for successive executions of the brace-delimitedblock.

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值