Static and Dynamic Memory Allocation in C

Last Updated : 6 Nov, 2025

Memory allocation in C determines how and when memory is assigned to variables and data structures during program execution. Efficient memory management is crucial for building optimized and bug-free programs. In C, memory allocation is categorized into two types:

memory_allocation
memory-allocation

How a Program Uses Memory

Before understanding static and dynamic allocation, it’s important to know how memory is organized in a program.

code_section
  • Code Section: Stores compiled instructions of the program.
  • Stack Memory: Stores local variables and function call data. Uses a Last-In, First-Out (LIFO) structure.
  • Heap Memory: Used for dynamic allocation. Memory is manually managed using pointers.
  • Data Segment: Holds global and static variables.

Note: Heap memory cannot be accessed directly; it can only be accessed through pointers.

1. Static Memory Allocation

Static memory allocation means that the memory for variables is allocated at compile-time, before the program starts executing.

  • Size and type of variables must be known at compile-time.
  • Memory remains fixed throughout the program execution.
  • Managed automatically by the compiler.
C++
#include <stdio.h>

int main(){
    
    // memory allocated statically
    int arr[5];  
    for (int i = 0; i < 5; i++) {
        arr[i] = i + 1;
        printf("%d ", arr[i]);
    }
    return 0;
}

Output
1 2 3 4 5 

Explanation: Here, the array arr[5] gets memory from the stack during compile-time. The size cannot change while the program runs.

Advantages

  • Simple usage.
  • Allocation and deallocation are done by the compiler.
  • Efficient execution time.
  • It uses stack data structures.

Disadvantage

  • Memory wastage problem.
  • Exact memory requirements must be known.
  • Memory can't be resized once after initialization.

Dynamic memory allocation

Dynamic memory allocation means that memory is allocated at runtime, i.e., while the program is executing. It allows flexible memory usage based on the program’s needs.

  • Memory is taken from the heap area.
  • Can allocate or release memory using library functions.
  • Requires pointers to access dynamically allocated blocks.

Common Functions For Allocate Memory Dynamically.

There are some functions available in the stdlib.h header which will help to allocate memory dynamically.

  • malloc(size): Allocates a block of memory of given size (in bytes).
  • calloc(n, size): Allocates memory for n elements and initializes all to 0.
  • realloc(ptr, size): Changes the size of the previously allocated memory block.
  • free(): Deallocates previously allocated memory, releasing it back to the heap.

Example: Using malloc() and free()

C
#include <stdio.h>
#include <stdlib.h>

int main(){
    
    int *ptr;
    int n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);
    
     // memory allocated dynamically
    ptr = (int*) malloc(n * sizeof(int));

    if (ptr == NULL) {
        printf("Memory not allocated.\n");
        return 1;
    }

    for (i = 0; i < n; i++) {
        ptr[i] = i + 1;
    }

    printf("Array elements: ");
    for (i = 0; i < n; i++) {
        printf("%d ", ptr[i]);
    }
    
    // releasing memory
    free(ptr);
    return 0;
}

Examples: Memory for n integers is allocated at runtime using malloc(). ptr points to the allocated memory block in the heap. After use, free(ptr) is called to prevent memory leaks.

Advantages:

  • Dynamic Allocation is done at run time.
  • We can allocate (create) additional storage whenever we need them.
  • Memory can be deallocated (free/delete) dynamic space whenever we are done with them.
  • Thus, one can always have exactly the amount of space required - no more, no less.
  • Memory size can be reallocated if needed.

Disadvantages:

  • As the memory is allocated during runtime, it requires more time.
  • Memory needs to be freed by the user when done. This is important as it is more likely to turn into bugs that are difficult to find.

Static vs Dynamic Memory Allocation

BasisStatic Memory AllocationDynamic Memory Allocation
When AllocatedCompile-timeRun-time
Memory Area UsedStackHeap
FlexibilityFixed sizeCan change size at runtime
DeallocationAutomatically doneMust be done manually using free()
SpeedFasterSlightly slower due to runtime allocation
Exampleint arr[10];int *arr = malloc(10 * sizeof(int));
Comment