0
0
CComparisonBeginner · 3 min read

Static vs Dynamic Memory Allocation in C: Key Differences and Usage

In C, static memory allocation reserves memory at compile time with fixed size, while dynamic memory allocation reserves memory at runtime with flexible size using functions like malloc. Static allocation is faster but less flexible; dynamic allocation allows variable-sized data but requires manual management.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of static and dynamic memory allocation in C.

FactorStatic Memory AllocationDynamic Memory Allocation
When memory is allocatedAt compile timeAt runtime
Memory sizeFixed and known before runningFlexible and can change during execution
Syntax exampleint arr[10];int *arr = malloc(10 * sizeof(int));
Memory lifetimeUntil program ends or scope endsUntil explicitly freed with free()
SpeedFaster allocationSlower due to runtime management
Use caseFor fixed-size dataFor variable-size or unknown data
⚖️

Key Differences

Static memory allocation happens when the compiler sets aside a fixed amount of memory before the program runs. This memory size cannot change while the program is running. Variables declared as arrays with fixed sizes or global/static variables use static allocation. It is simple and fast because the memory is ready when the program starts.

Dynamic memory allocation happens during program execution using functions like malloc, calloc, and realloc. This allows the program to request memory as needed, which is useful when the size of data is not known in advance or can change. However, the programmer must manually free this memory using free to avoid memory leaks.

In summary, static allocation is less flexible but safer and faster, while dynamic allocation is flexible but requires careful management to avoid errors like memory leaks or dangling pointers.

⚖️

Code Comparison

This example shows how to create an integer array of size 5 using static memory allocation.

c
#include <stdio.h>

int main() {
    int arr[5];
    for (int i = 0; i < 5; i++) {
        arr[i] = i * 10;
    }
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
Output
0 10 20 30 40
↔️

Dynamic Memory Allocation Equivalent

This example shows how to create the same integer array of size 5 using dynamic memory allocation.

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

int main() {
    int *arr = malloc(5 * sizeof(int));
    if (arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    for (int i = 0; i < 5; i++) {
        arr[i] = i * 10;
    }
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    free(arr);
    return 0;
}
Output
0 10 20 30 40
🎯

When to Use Which

Choose static memory allocation when you know the exact size of data at compile time and want faster, simpler code without worrying about manual memory management. It is ideal for fixed-size arrays and global/static variables.

Choose dynamic memory allocation when the data size can change or is unknown before running the program. It is essential for flexible data structures like linked lists, resizable arrays, or when working with large data that may not fit in static memory.

Always remember to free dynamically allocated memory to avoid leaks.

Key Takeaways

Static memory allocation reserves fixed memory at compile time and is faster but less flexible.
Dynamic memory allocation reserves memory at runtime and allows flexible sizes but requires manual freeing.
Use static allocation for fixed-size data and dynamic allocation for variable or unknown sizes.
Always check if dynamic memory allocation succeeds and free allocated memory to prevent leaks.
Static allocation is simpler; dynamic allocation is powerful but needs careful management.