Static memory allocation in C


Reading time: 20 minutes | Coding time: 2 minutes

Static memory allocation is an allocation technique which allocates a fixed amount of memory during compile time and the operating system internally uses a data structure known as Stack to manage this.

Background

Memory is central to any computing system and its architecture determines the performance of any process. While building system, one of the fundamental task is to allocate memory.

There are two types of memory allocated to a program:

  • Stack memory
  • Heap memory

Stack memory is allocated during compilation time execution. This is known as static memory allocation.

Whereas, heap memory is allocated at run-time compilation. This is know as dynamic memory allocation.

memory

Static memory allocation

In Static Memory Allocation the memory for your data is allocated when the program starts. The size is fixed when the program is created. It applies to global variables, file scope variables, and variables qualified with static defined inside functions. This memory allocation is fixed and cannot be changed, i.e. increased or decreased after allocation. So, exact memory requirements must be known in advance.

Key features:

  • Variables get allocated permanently
  • Allocation is done before program execution
  • It uses the data structure called stack for implementing static allocation
  • Less efficient
  • There is no memory reusability

Example

  • All the variables in the program below are statically allocated.
void play
{
   int a; 
}
int main() 
{
   int b; 
   int c[10]; 
   return 1;
}
  • In this type of allocation, you strictly allocate memory for your data at compile time. This is also called simple memory allocation. It is mostly used and very easy to application.

Deletion of allocated memory

Deletion of memory allocated to a program is as important as allocation otherwise it results in memory leakage. Statically allocated memory is automatically released on the basis of scope, i.e., as soon as the scope of the variable is over, memory allocated get freed.

Static Variables

As the name suggests, the value of static variables persists until the end of the program. A variable can be declared static using the keyword static like

static int x;
static float y;

A static variable may be either an internal type or an external type depending on the place of declaration.

Internal static variables are those which are declared inside a function. The scope of static variables extend up to the end of the function in which they are defined. Therefore, internal static variables are similar to auto variables, except that they remain in existence(alive) throughout the remainder of the program.For example, it can be used to count the number of calls made to a function

Example

void stat(void);

int main()
{
   int i;
   for(i=1; i<=3 ; i++)
      stat();
   return 1;
}
void stat(void) 
{
   static int x = 0;
   x = x+1;
   printf("x = %d/n", x); 
}

Output

x = 1
x = 2
x = 3

An external static variable is declared outside of all functions and is available to all the functions in that program. The difference between a static external variable and a simple external variable is that the static external variable can be accessed by other files.

Advantages of Static memory allocation

  • Simplicity of usage.
  • Efficient execution time.
  • Need not worry about memory allocation/re-allocation/freeing of memory
  • Variables remain permanently allocated.

Disadvantages of Static memory allocation

  • Main disadvantage is wastage of memory.
  • Memory can't be freed when it is no longer needed.