Static memory allocation refers to the process of allocating 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.[1]
An application of this technique involves a program module (e.g.
function or subroutine)
declaring static data locally, such that these data are inaccessible in other
modules unless references to it are passed as parameters or returned. A single
copy of static data is retained and accessible through many calls to the
function in which it is declared. Static memory allocation therefore has the
advantage of modularising data within a program design in the situation where
these data must be retained through the runtime of the program.
The use of static variables within a class in object oriented programming enables a single copy of such data to
be shared between all the objects of that class.
Object constants known at compile-time, like string literals, are usually allocated statically. In
object-oriented programming, the virtual method tables of classes are usually allocated statically. A statically defined
value can also be global in its scope ensuring
the sameimmutable value is used throughout
a run for consistency.
References
1. Static memory allocation: 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.
2. Dynamic memory allocation: It uses functions such as malloc( ) or calloc( ) to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time.
What is
static memory allocation and dynamic memory allocation?
Static Memory Allocation: Memory
is allocated for the declared variable by the compiler. The address can be
obtained by using ‘address of’ operator and can be assigned to a pointer. The
memory is allocated during compile time. Since most of the declared variables
have static memory, this kind of assigning the address of a variable to a
pointer is known as static memory allocation.
Dynamic Memory Allocation:
Allocation of memory at the time of execution (run time) is known as dynamic
memory allocation. The functions calloc() and malloc() support allocating of
dynamic memory. Dynamic allocation of memory space is done by using these functions
when value is returned by functions and assigned to pointer
variables.
Rationale
The C programming language manages memory statically, automatically, or dynamically.
Static-duration variables are allocated in main memory, usually along with the
executable code of the program, and persist for the lifetime of the program;
automatic-duration variables are allocated on the stack and come and go as functions are called and
return. For static-duration and automatic-duration variables, the size of the
allocation is required to be compile-time constant (before C99, which
allows variable-length automatic arrays[5]). If the
required size is not known until run-time (for example, if data of arbitrary size is
being read from the user or from a disk file), then using fixed-size data
objects is inadequate.
The lifetime of allocated memory
is also a concern. Neither static- nor automatic-duration memory is adequate
for all situations. Automatic-allocated data cannot persist across multiple
function calls, while static data persists for the life of the program whether
it is needed or not. In many situations the programmer requires greater
flexibility in managing the lifetime of allocated memory.
These limitations are avoided by
using dynamic memory allocation in which memory is more explicitly (but more
flexibly) managed, typically, by allocating it from the heap, an area of memory
structured for this purpose. In C, the library function
malloc
is used to allocate a block of memory on the
heap. The program accesses this block of memory via a pointer that malloc
returns. When the memory is no longer needed,
the pointer is passed to free
which deallocates the memory so that it can
be used for other purposes.
Some platforms provide library
calls which allow run-time dynamic allocation from the C stack rather than the
heap (e.g. Unix
all
ction ends. The need for this
is lessened by changes in the C99 standard, which added support for variable-leoca()
,[6] Microsoft Windows CRTL's malloca()
[7]). This memory is automatically freed when the calling funngth arrays of block scope having sizes determined at
runtime.