Difference between Compile Time and Run Time Memory Allocation in C

Memory allocation is a crucial part of any C program. Whether you’re declaring an integer variable, creating an array, or dynamically allocating space for a data structure, you’re working with memory. In C, memory allocation happens in two major ways:

  • Compile-Time Memory Allocation (also known as Static Memory Allocation)
  • Run-Time Memory Allocation (also known as Dynamic Memory Allocation)

What is Compile-Time (Static) Memory Allocation?

Compile-time memory allocation, also known as static memory allocation, occurs when memory for variables is allocated during the compilation of the program. This means that the size and type of variables must be known beforehand.​

Key Characteristics:

  • You must know the size of variables before compiling the program.
  • The program sets the memory location and doesn’t change it.
  • It runs faster because it allocates memory during compilation.
  • It doesn’t offer flexibility for dynamic memory allocation.
  • After allocating memory, the program can’t change it while running.

Example:

#include <stdio.h>
 
int main() {
    int x = 10; // Allocated during compile time
    printf("%d", x);
    return 0;
}

This program creates an int variable x with the value 10. The memory is set at compile time, so it’s fast. It then prints the value using printf.

advertisement

What is Run-Time (Dynamic) Memory Allocation?

Run-time memory allocation, or dynamic memory allocation, happens during the execution of the program. It allows for allocating memory when the size of the data structure cannot be determined at compile-time.

Key Characteristics:

  • Allocation happens while the program is running.
  • Memory size can be set dynamically, based on user input or other conditions.
  • Uses the heap area of memory.
  • Requires manual memory management (you must free memory yourself).
  • Uses functions like malloc(), calloc(), realloc(), and free() from <stdlib.h>.

Example:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
#include <stdio.h>
#include <stdlib.h>
 
int main() {
    int *x;
    x = (int*)malloc(sizeof(int)); // Allocated during runtime
    if (x != NULL) {
        *x = 10;
        printf("%d", *x);
        free(x); // Free allocated memory
    }
    return 0;
}

This program uses malloc to allocate memory for an integer while the program is running. It checks if the allocation worked, sets the value to 10, and prints it. Then, it frees the memory using free to prevent memory leaks.

Differences Between Compile-Time and Run-Time Memory Allocation

Feature Compile Time Memory Allocation Run Time Memory Allocation
Allocation Time During compilation During execution
Flexibility Fixed size, cannot be changed Flexible, can allocate as needed
Memory Location Stack or Data Segment Heap
Allocation Responsibility Handled by compiler Handled manually by programmer
Re-sizing Memory Not possible Possible using realloc()
Functions Used None (declarations) malloc(), calloc(), realloc(), free()
Example int a[10]; int *a = malloc(10 * sizeof(int));
Speed Faster (less overhead) Slower (due to system calls)
Risk of Leaks Minimal Higher if not properly managed

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.