Static memory allocation is a method of allocating memory where the size and storage requirements of an object are determined before or as part of program startup, rather than being requested dynamically during program execution. In C, objects with static storage duration exist for the entire execution of the program.
Static allocation is commonly associated with variables declared at
file scope, variables declared with the
static keyword, and certain fixed-size arrays. However, it is
important to distinguish static memory allocation from the
static keyword itself. A local variable declared with
static has static storage duration, but not every form of
compile-time-sized array is necessarily a "static variable."
int numbers[10];
The array above contains space for 10 integers, and its size
cannot be changed using operations such as realloc(). The
number of elements is fixed for that particular array object.
Another example is:
static int count = 0;
Here, count has static storage duration. It
exists from program startup until program termination and retains its value
throughout that period.
malloc(), calloc(), and realloc()
are not required to obtain storage for such objects.
| Static Allocation | Dynamic Allocation |
|---|---|
| Storage requirements are fixed for the object's lifetime. | Storage can be requested and released during program execution. |
Does not require malloc() or calloc().
|
Commonly uses malloc(), calloc(),
realloc(), and free().
|
| The object's lifetime may extend for the entire program when it has static storage duration. | The allocated memory remains available until it is explicitly released or the program terminates. |
| The storage size cannot be resized for the same object. |
Allocated storage can potentially be resized using
realloc().
|
Example: static int count;
|
Example: int *p = malloc(10 * sizeof(int));
|
A common misconception is that static memory is allocated on the stack or the heap. Static-storage objects are generally associated with program data sections such as initialized data and zero-initialized (BSS) storage. They are not ordinary automatic stack variables and are not dynamically allocated from the heap.
For example:
static int total = 100;
total has static storage duration. Its exact physical memory
placement is implementation-dependent, but it is not allocated using
malloc().
#include
void counter()
{
static int count = 0;
count++;
printf("Count = %d\n", count);
}
int main()
{
counter();
counter();
counter();
return 0;
}
Output:
Count = 1
Count = 2
Count = 3
The variable count is initialized only once. Because it has
static storage duration, its value is retained between calls to
counter().
A fixed-size array such as:
int a[10];
does have a fixed number of elements, but it should not automatically be
called a static variable. If it is declared inside a
function without static, it normally has automatic storage
duration:
void example()
{
int a[10];
}
If we want the array to have static storage duration, we can explicitly declare it as:
void example()
{
static int a[10];
}
In this case, the array exists for the entire execution of the program, while its name is accessible only within the function.
Static memory allocation can be understood as allocating storage for an object without requesting memory dynamically at runtime. The storage requirements remain fixed, and objects with static storage duration remain available throughout the execution of the program.
Therefore, remember:
Static storage duration → Exists for the entire program
execution.
Dynamic allocation → Memory is requested and managed at
runtime.