In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, 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
In computer programming, an automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The scope is the lexical context, particularly the function or block in ...
s, whose storage is
stack allocated and deallocated on the
call stack
In computer science, a call stack is a Stack (abstract data type), stack data structure that stores information about the active subroutines and block (programming), inline blocks of a computer program. This type of stack is also known as an exe ...
; 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 variable
In computer science, a local variable is a variable that is given ''local scope''. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with ...
s are generally automatic, but may be static.
In general, is the allocation of memory at
compile time
In computer science, compile time (or compile-time) describes the time window during which a language's statements are converted into binary instructions for the processor to execute. The term is used as an adjective to describe concepts relat ...
, before the associated program is executed, unlike
dynamic memory allocation
Memory management (also dynamic memory management, dynamic storage allocation, or dynamic memory allocation) is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dyna ...
or
automatic memory allocation 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 family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a ...
(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 programming 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''.
The
static
keyword is used in C and related languages both for static variables and other concepts.
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
In computer science, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instruction (computer science), in ...
or
library
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
) is
loaded into memory, static variables are stored in the
data segment of the program's
address space
In computing, an address space defines a range of discrete addresses, each of which may correspond to a network host, peripheral device, disk sector, a memory cell or other logical or physical entity.
For software programs to save and retrieve ...
(if initialized), or the
BSS segment (if uninitialized), and are stored in corresponding sections of
object file
An object file is a file that contains machine code or bytecode, as well as other data and metadata, generated by a compiler or assembler from source code during the compilation or assembly process. The machine code that is generated is kno ...
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. 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, a local variable is a Variable (programming), variable that is given ''local scope (programming), scope''. A local variable reference in the subroutine, function or block (programming), block in which it is declared overrides ...
,'' 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()
int main()
Object-oriented programming
In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, there is also the concept of a ''
static member variable'', which is a "
class variable" of a statically defined class, i.e., a
member variable
In object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called ...
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
string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where , "foo ...
s, are usually allocated statically. In object-oriented programming, the
virtual method table
In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding).
...
s of classes are usually allocated statically. A statically defined value can also be
global
Global may refer to:
General
*Globe, a spherical model of celestial bodies
*Earth, the third planet from the Sun
Entertainment
* ''Global'' (Paul van Dyk album), 2003
* ''Global'' (Bunji Garlin album), 2007
* ''Global'' (Humanoid album), 198 ...
in its scope ensuring the same
immutable value is used throughout a run for consistency.
See also
*
Constant (computer programming)
In computer programming, a constant is a Value (computer science), value that is not altered by the Computer program, program during normal Execution (computing), execution. When associated with an Identifier (computer languages), identifier, a co ...
*
Global variable
*
Static method
*
Thread-local storage
In computer programming, thread-local storage (TLS) is a memory management method that uses static memory allocation, static or global computer storage, memory local to a thread (computing), thread. The concept allows storage of data that appear ...
Notes
References
*
*''
The C++ Programming Language'' (special edition) by
Bjarne Stroustrup (Addison Wesley, 2000; )
{{DEFAULTSORT:Static Memory Allocation
Memory management
Variable (computer science)