What Are Static Variables?

Static variable ( Static Variable ) in the field of computer programming refers to a type of variable that the system statically allocates (that is, it does not change the allocation in runtime) the storage space for the program before it is executed. Corresponding to them are automatic variables (ie local variables) that only exist temporarily at runtime and some objects that obtain storage space by dynamic allocation. The storage space of automatic variables is allocated and released on the call stack.

The term "static variable" has two confusing definitions:
  1. Language-agnostic common definition: same as program
    Static variables can also be used for storage
    In C language and programming languages such as C ++ and Objective-C derived from it, "static" is a reserved word used to control the variable's life cycle and connection mode (that is, its scope and visibility). Specifically, just like the extern, auto and register reserved words in the C language, static is also a storage class (the "class" here is different from the definition of "class" in object-oriented languages). Each variable and function has one of the above storage class identifiers. If the storage class is not clearly identified in the declaration, the default storage class will be selected according to the context when compiling, such as all file-level variables in the source file. The default storage class is extern, and the variables in the function body correspond to auto. The properties of each storage class are listed in the following table. [1]
    Storage class name Life cycle Scope
    extern Static (released after program ends) External (whole program)
    static Static (released after program ends) Internal (translation unit only, usually a single source file)
    auto, register Function call (released after the call ends) no
    It is easy to see that variables whose storage class is extern (including the file-level variables mentioned above without explicitly declaring the storage class) match definition 1 of the static variable described in the previous paragraph, but not definition 2.

    The role of static variables in different situations

    In addition to clearly identifying the variable's life cycle, declaring the variable as a static storage class will also have some special effects depending on the variable attributes:
    • For static global variables, the scope of file-level variables and functions declared statically for a certain source file is limited to the file (only visible in the file), that is, "internal link", so it can be used to limit variables Scope
    • For static local variables, although the variables declared static in the function have the same scope as the automatic local variables (that is, the scope is limited to the function), the storage space is obtained by static allocation instead of the default automatic allocation method. The storage area is different. (Generally speaking, the storage space is statically allocated in the program data segment at compile time, and the allocation is effective at one time. In the automatic allocation, the storage space is allocated on the call stack and only when called. Allocation and release), and the value of the variable is always the same between the two calls; it must be noted that static local variables can be initialized only once, which is guaranteed by the compiler.
    • For static member variables, in C ++, member variables declared statically in the class definition belong to class variables, that is, they are shared among all class instances, and the opposite is process variables.

    C Example of static variable C

    In C, a program with static variables is as follows: [1]
      #include <stdio.h> void func () {static int x = 0; // In three calls to func, x is initialized only once printf ("% d \ n", x); // Output of x Value x = x + 1;} int main (int argc, char * const argv []) {func (); // output 0 func (); // output 1 func (); // output 2 return 0;}
    

    C++ C ++ example of static variables

    In C ++, a program with a class containing private static internal variables looks like this:
      class Request {
    
     private:
         static int count; // cannot be called externally
         string url; // can only be called by member functions public:
         Request ()
         {
             count ++;
         }
    
         string getUrl () const {return url;}
    
         void setUrl (string value)
         {
             url = value;
         }
    
         static int getCount ()
         {
             return count;
         }
     };
    
     int Request :: count = 0; // count can be initialized outside the class declaration 

    IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?