Static Memory Allocation

What is Static Memory Allocation?

With respect to computer memory, static memory allocation refers to the specific part of the main memory of the system that is reserved for an application or a program when it is loaded for the first time.

In other words, it signifies the space in the memory that is reserved for a particular program, only one program can use this space at a time, which can be allocated again only when the particular program or application is closed.

Understanding Static Memory Allocation

What is Static Memory Allocation

Also referred to as compile-time memory allocation, static memory allocation allows the program to use a fixed amount of memory.

From a technical point of view, it is the compiler that assigns the declared static variables. These variables are accessible if an ‘address of’ operator is allotted to a pointer variable.

Steps

While allocating a memory space to a program statically, a few specific steps are followed as under:

The final allocation of memory space is then made, and as you can see, it is done entirely on the basis of a couple of assumptions.

In such a situation, there can be two evident possibilities, such as:

Variables

Just as the name implies, the variables used in static memory allocation are also static, and these variables continue to exist until the completion of the program or application.

Typically, these static variables in this type of memory allocation are declared by using the keyword ‘static’ in commands such as:

The static variables can be either of the following:

However, which one it will be typically depends on the place of assertion.

Normally, each of these two types of variables has different features as explained hereunder:

Internal static variables

External static variables

Static Memory Allocation Example

One of the most significant examples of static memory allocation is the declaration of a program in an array or for a variable in the program.

Here the memory is assigned during the compile time and the address can be gotten with the help of an ‘&’ operator. It can also be assigned to a pointer.

When the ultimate size of the variable is ascertained even before the program is run by using a stack for maintaining the allocation and cannot be changed, it is supposed to be a static memory allocation.

It is also referred to as compile-time memory allocation.

Advantages

Disadvantages

Why is Static Memory Allocation Fast?

The most significant factor that makes the static memory allocation much faster and more efficient is that memory is typically allocated to a process in this method even before it starts running.

This means that there is no overhead associated with the operations of memory allocation, even when the program is running, which eventually expedites the process significantly.

And, when you compare it with dynamic memory allocation, it is faster because the compiler needs to work one step less while hard-coding the variable data into it.

For example, it simply has to estimate the offset to a specific element of an array to be accessed from the static address delineated at compile time.

Typically, it does not have to read the variable data containing the pointer to the opening of the array.

Moreover, the space allocated to a program cannot be changed during operation which also helps it to operate faster.

Is Static Memory in Stack or Heap?

Typically, static memory allocation is related to stack memory and is smaller than heap memory. It is usually referred to as compile time allocation.

Heap memory, on the other hand, is typically related to dynamic memory allocation.

It is larger than stack memory and is usually referred to as runtime allocation.

Conclusion

Static memory allocation refers to the process of allocating memory space to a program during compilation based on an assumption of the amount of memory it will need during the entire operation.

It is better than dynamic memory allocation in quite a few respects, but it also comes with a fair share of limitations.