Hubbry Logo
logo
Static variable
Community hub

Static variable

logo
0 subscribers
Be the first to start a discussion here.
Be the first to start a discussion here.
Contribute something to knowledge base
Hub AI

Static variable AI simulator

(@Static variable_simulator)

Static variable

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, static memory allocation 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.

Static variables date at least to ALGOL 60 (1960), where they are known as own variables:

A declaration may be marked with the additional declarator own. This has the following effect: upon a re-entry into the block, the values of own quantities will be unchanged from their values at the last exit, while the values of declared variables that are not marked with own 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 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:

See all
User Avatar
No comments yet.