FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Static_variable#Static_Variables_as_Class_Variables

Static variable

Contents
Addressing
Scope
Object-oriented programming

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. Static variables may also be given thread-local storage to ensure that each thread gets an independent instance of the variable.

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.

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

(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 an automatic variable (local, stack-allocated), not for heap-allocated objects, which is the current use of the term dynamic allocation.

Addressing

edit

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

edit
#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 1 func();// prints 2 func();// prints 3 func();// prints 4 func();// prints 5 return0; }

Object-oriented programming

edit
static function-local variable) is guaranteed to be thread-safe, even without the usage of manual locks; this feature is sometimes colloquially known as "magic statics". However, this feature depends on the C++ runtime.

In object-oriented programming, static variables are often used to implement the singleton pattern.

"Revised Report on the Algorithmic Language Algol 60". www.masswerk.at. Retrieved 2026-10-25.
  • ↑ "Static storage duration - cppreference.com". en.cppreference.com. Archived from the original on 2026-08-09. Retrieved 2026-11-03.
  • ↑

  • View original on Wikipedia | Back | FazBrowse Home