Static variable
≈ 5 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program. This is in contrast to shorter-lived automatic variables, whose storage is stack allocated and deallocated on the call stack; and in contrast to dynamically allocated objects, whose storage is allocated and deallocated in heap memory. Variable lifetime is contrasted with scope (where a variable can be used): "global" and "local" refer to scope, not lifetime, but scope often implies lifetime. In many languages, global variables are always static, but in some languages they are dynamic, while local variables are generally automatic, but may be static. In general, is the allocation of memory at compile time, before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at run time. The static keyword is used in C and related languages both for static variables and other concepts. In Rust, an object's lifetime can be marked static using the static keyword or with a &'static lifetime annotation.
In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program. This is in contrast to shorter-lived automatic variables, whose storage is stack allocated and deallocated on the call stack; and in contrast to dynamically allocated objects, whose storage is allocated and deallocated in heap memory. Variable lifetime is contrasted with scope (where a variable can be used): "global" and "local" refer to scope, not lifetime, but scope often implies lifetime. In many languages, global variables are always static, but in some languages they are dynamic, while local variables are generally automatic, but may be static. In general, is the allocation of memory at compile time, before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at run time. The static keyword is used in C and related languages both for static variables and other concepts. In Rust, an object's lifetime can be marked static using the static keyword or with a &'static lifetime annotation. ==History== Static variables date at least to ALGOL 60 (1960), where they are known as own variables: This definition is subtly different from a static variable: it only specifies behavior, and hence lifetime, not storage: an own variable can be allocated when a function is first called, for instance, rather than at program load time. The use of the word static to refer to these variables dates at least to BCPL (1966), and has been popularized by the C language, which was heavily influenced by BCPL. The BCPL definition reads: Note that BCPL defined a "dynamic data item" for what is now called an automatic variable (local, stack-allocated), not for heap-allocated objects, which is the current use of the term dynamic allocation. ==Addressing== The absolute address addressing mode can only be used with static variables, because those are the only kinds of variables whose location is known by the compiler at compile time. When the 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. ==Scope== 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 has global scope and thus is in context throughout the program, and a static local variable, which has local scope. A static local variable is different from a local variable as a static local variable is initialized only once no matter how many times the function in which it resides is called and its value is retained and accessible through many calls to the function in which it is declared, e.g. to be used as a count variable. 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. ===Example=== An example of a static local variable in C: include void func() { static int x = 0; // x is initialized only once across five calls of func() and the variable // will get incremented five times after these calls. The final value of x // will be 5. x++; printf("%d\n", x); // outputs the value of x } int main() { func(); // prints 1 func(); // prints 2 func(); // prints 3 func(); // prints 4 func(); // prints 5 return 0; } ==Notes==
Text derived from https://en.wikipedia.org/w/index.php?oldid=1376095955 under CC BY-SA 4.0; modifications have been made.