HOME

TheInfoList




In
computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, generating algorithms, Profilin ...
, 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 variable __NOTOC__ In computer programming, an automatic variable is a local Variable (programming), variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The Scope (computer science), scope is t ...
s, whose storage is stack allocated and deallocated on the
call stack In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of com ...
; and in contrast to
objects Object may refer to: General meanings * Object (philosophy) An object is a philosophy, philosophical term often used in contrast to the term ''Subject (philosophy), subject''. A subject is an observer and an object is a thing observed. For mo ...
, whose storage is dynamically allocated and deallocated in
heap memory Memory management is a form of Resource management (computing), resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their re ...
.
Variable lifetime In computer programming, a variable or scalar is an abstract storage location paired with an associated symbol, symbolic name, which contains some known or unknown quantity of information referred to as a ''value (computer science), value''; or i ...
is contrasted with
scope Scope or scopes may refer to: People with the surname * Jamie Scope (born 1986), English footballer * John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution Arts, media, and entertainment * CinemaS ...
(where a variable can be used): "global" and "local" refer to scope, not lifetime, but scope often implies lifetime. In many languages,
global variable In computer programming Computer programming is the process of designing and building an executable In computing, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes ...
s are always static, but in some languages they are dynamic, while
local variable In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of compu ...
s are generally automatic, but may be static. In general, is the allocation of memory at
compile time In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of co ...
, before the associated program is executed, unlike
dynamic memory allocation Memory management is a form of resource management In organizational studies Organizational studies is "the examination of how individuals construct organizational structures, processes, and practices and how these, in turn, shape social relat ...
or
automatic memory allocation __NOTOC__ In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analys ...
where memory is allocated as required at run time.


History

Static variables date at least to
ALGOL 60 ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL ALGOL (; short for "Algorithmic Language") is a family of imperative Imperative may refer to: *Imperative mood, a grammatical mood (or mode) expressing commands, dir ...
(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 BCPL ("Basic Combined Programming Language") is a procedural, imperative Imperative may refer to: *Imperative mood, a grammatical mood (or mode) expressing commands, direct requests, and prohibitions *Imperative programming, a programming pa ...
(1966), and has been popularized by the
C programming language C (, as in the letter ''c'') is a general-purpose, procedural computer programming language A programming language is a formal language comprising a Instruction set architecture, set of instructions that produce various kinds of Input/outp ...
, 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''. The static keyword is used in C and related languages both for static variables and other concepts.


Addressing

The
absolute address In computing, a memory address is a reference to a specific computer memory, memory location used at various levels by software and computer hardware, hardware. Memory addresses are fixed-length sequences of Numerical digit, digits conventionally ...
addressing mode Addressing modes are an aspect of the instruction set architecture In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techni ...
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 In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and softwar ...
of the program's address space (if initialized), or the
BSS segment In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, genera ...
(if uninitialized), and are stored in corresponding sections of
object file An object file is a computer file containing object code In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and ...
s 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 Scope or scopes may refer to: People with the surname * Jamie Scope (born 1986), English footballer * John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution Arts, media, and entertainment * CinemaS ...
. 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 In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of , , a ...
,'' 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 Module, modular and modularity may refer to the concept of modularity Broadly speaking, modularity is the degree to which a system A system is a group of Interaction, interacting or interrelated elements that act according to a set of rule ...
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() int main()


Object-oriented programming

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. Some paradigms are concerned mai ...
, there is also the concept of a '' static member variable'', which is a "
class variable In object-oriented programming Object-oriented programming (OOP) is a programming paradigm Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. S ...
" of a statically defined class, i.e., a
member variable In object-oriented programming Object-oriented programming (OOP) is a programming paradigm Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. S ...
of a given class which is shared across all instances (objects), and is accessible as a member variable of these objects. 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. Object constants known at compile-time, such as
string literal A string literal or anonymous string is a type of literal Literal may refer to: * Interpretation of legal concepts: ** Strict constructionism ** The plain meaning rule (a.k.a. "literal rule") * Literal (mathematical logic), certain logical r ...
s, are usually allocated statically. In object-oriented programming, the
virtual method table A virtual method table (VMT), virtual function table, virtual call table, dispatch tableIn computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well a ...
s of classes are usually allocated statically. A statically defined value can also be
global Global means of or referring to a globe A globe is a spherical physical model, model of Earth, of some other astronomical object, celestial body, or of the celestial sphere. Globes serve purposes similar to maps, but unlike maps, they do not dis ...
in its scope ensuring the same
immutable In object-oriented computer programming, object-oriented and Functional programming, functional programming, an immutable object (unchangeable object) is an object (computer science), object whose state cannot be modified after it is created.Goetz ...
value is used throughout a run for consistency.


See also

*
Constant (computer programming) In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, genera ...
*
Global variable In computer programming Computer programming is the process of designing and building an executable In computing, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes ...
*
Static method A method in object-oriented programming Object-oriented programming (OOP) is a programming paradigm Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple pa ...
*
Thread-local storage Thread-local storage (TLS) is a computer programming method that uses static or global memory Memory is the faculty of the brain A brain is an organ Organ may refer to: Biology * Organ (anatomy) An organ is a group of Tissue (biol ...


Notes


References

* *''
The C++ Programming Language ''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
'' (special edition) by
Bjarne Stroustrup Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist A computer scientist is a person who has acquired the knowledge of computer science, the study of the theoretical foundations of information and computation and t ...

Bjarne Stroustrup
(Addison Wesley, 2000; ) {{DEFAULTSORT:Static Memory Allocation Memory management Variable (computer science)