Incomputer programming, astatic variable is avariable that has beenallocated "statically", meaning that itslifetime (or "extent") is the entire run of the program. This is in contrast to shorter-livedautomatic variables, whose storage isstack allocated and deallocated on thecall stack; and in contrast todynamically allocated objects, whose storage is allocated and deallocated inheap memory.
Variable lifetime is contrasted withscope (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, whilelocal variables are generally automatic, but may be static.
In general,static memory allocation is the allocation of memory atcompile time, before the associated program is executed, unlikedynamic memory allocation orautomatic memory allocation where memory is allocated as required atrun time.[1]
Static variables date at least toALGOL 60 (1960), where they are known asown variables:
A declaration may be marked with the additional declaratorown. This has the following effect: upon a re-entry into the block, the values ofown quantities will be unchanged from their values at the last exit, while the values of declared variables that are not marked withown is undefined.
— Revised report on ALGOL 60, section "5. Declarations", p. 14
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 wordstatic to refer to these variables dates at least toBCPL (1966), and has been popularized by theC programming language, which was heavily influenced by BCPL. The BCPL definition reads:
(1) Static data items:
Those data items whose extents lasts as long as the program execution time; such data items have manifest constant Lvalues. Every static data item must have been declared either in a function or routine definition, in a global declaration or as a label set by colon.— The BCPL Reference Manual, 7.2 Space Allocation and Extent of Data Items
Note that BCPL defined a "dynamic data item" for what is now called anautomatic variable (local, stack-allocated), not for heap-allocated objects, which is the current use of the termdynamic allocation.
Thestatic
keyword is used in C and related languages both for static variables and other concepts.
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(June 2023) (Learn how and when to remove this message) |
Theabsolute addressaddressing 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 orlibrary) isloaded into memory, static variables are stored in thedata segment of the program'saddress space (if initialized), or theBSS segment (if uninitialized), and are stored in corresponding sections ofobject files prior to loading.
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(June 2023) (Learn how and when to remove this message) |
In terms ofscope and extent, static variables have extent the entire run of the program, but may have more limitedscope. A basic distinction is between astatic global variable, which has global scope and thus is in context throughout the program, and astatic 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 havemodule scope or some variant, such asinternal linkage inC, which is a form of file scope or module scope.
An example of a static local variable in C:
#include<stdio.h>voidFunc(){staticintx=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|}intmain(){Func();// prints 1Func();// prints 2Func();// prints 3Func();// prints 4Func();// prints 5return0;}
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(June 2023) (Learn how and when to remove this message) |
Inobject-oriented programming, there is also the concept of astatic member variable, which is a "class variable" of a statically defined class, i.e., amember variable of a given class which is shared across allinstances (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 asstring literals, are usually allocated statically. In object-oriented programming, thevirtual method tables of classes are usually allocated statically. A statically defined value can also beglobal in its scope ensuring the sameimmutable value is used throughout a run for consistency.
The compiler allocates required memory space for a declared variable. By using the addressof operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variables have static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time.