In C programming, memory allocation is a key concept that determines how memory is assigned to variables or data structures during program execution. It plays a crucial role when working with linked lists, which are dynamic data structures. Here’s a complete explanation of Dynamic and Static Memory Allocation, especially in the context of Linked Lists.
Static Memory Allocation in C
Static Memory Allocation means memory for variables is allocated at compile time. Once allocated, the size cannot be changed during runtime.
Key Features:
- Done at compile time.
- Memory size is fixed and known in advance.
- Allocated on the stack.
- No use of dynamic functions like malloc() or calloc().
Example:
int quiz[10]; // Static allocation
In this example, memory for 10 integers is allocated when the program starts, and it cannot grow or shrink during execution.
Dynamic Memory Allocation in C
Dynamic Memory Allocation means memory is allocated at runtime, using special functions. This is useful when the size of data is not known in advance.
Key Features:
- Done at runtime.
- Memory can be reallocated.
- Allocated on the heap.
- Managed using functions like malloc(), calloc(), realloc(), and free().
Example:
int *quiz = (int*)malloc(10 * sizeof(int));
Static Memory Allocation in Linked Lists
In static memory allocation, memory for all nodes in the linked list is allocated before the program runs. This is done using fixed-size variables or arrays.
Limitations:
- Fixed number of nodes.
- Memory is allocated on the stack.
- Not flexible or scalable.
Example: Static Linked List with 3 Nodes
#include <stdio.h> struct Node { int quiz; struct Node* test; }; int main() { struct Node certification, assignment, exam; certification.quiz = 10; certification.test = &assignment; assignment.quiz = 20; assignment.test = &exam; exam.quiz = 30; exam.test = NULL; struct Node* mcqs = &certification; // Traversing the static linked list while (mcqs != NULL) { printf("%d -> ", mcqs->quiz); mcqs = mcqs->test; } return 0; }
Output:
10 -> 20 -> 30 ->
This C program creates and traverses a simple static linked list. It uses three nodes with values 10, 20, and 30, linked using pointers. The list starts from the first node and moves through each one using a loop. Each value is printed with an arrow, ending with NULL. The output is: 10 -> 20 -> 30 ->.
Dynamic Memory Allocation in Linked Lists
In dynamic memory allocation, each node is created at runtime using malloc() or calloc() from the heap memory.
Advantages:
- Scalable (add/delete nodes at runtime).
- Efficient memory usage.
- Required for real-world linked list operations.
Example: Dynamically Creating a Linked List with 3 Nodes
#include <stdio.h> #include <stdlib.h> struct Node { int quiz; struct Node* test; }; int main() { struct Node* certification = (struct Node*)malloc(sizeof(struct Node)); struct Node* assignment = (struct Node*)malloc(sizeof(struct Node)); struct Node* exam = (struct Node*)malloc(sizeof(struct Node)); certification->quiz = 100; certification->test = assignment; assignment->quiz = 200; assignment->test = exam; exam->quiz = 300; exam->test = NULL; struct Node* mcqs = certification; // Traversing the dynamic linked list while (mcqs != NULL) { printf("%d -> ", mcqs->quiz); mcqs = mcqs->test; } // Freeing dynamically allocated memory free(certification); free(assignment); free(exam); return 0; }
Output:
100 -> 200 -> 300 ->
This C program creates a dynamic singly linked list using malloc() to allocate memory for three nodes. Each node stores quiz marks: 100, 200, and 300, and is linked to the next. A loop traverses the list, printing each value followed by ->, ending with NULL. After traversal, it properly frees the allocated memory. The output is: 100 -> 200 -> 300 ->.
Why Static Allocation Doesn’t Work Well for Linked Lists
- Linked lists are meant to grow or shrink dynamically at runtime.
- With static memory, you’d have to fix the number of nodes beforehand, which defeats the purpose.
- Also, linking statically allocated nodes would require maintaining references manually and carefully, which is unnecessarily complex and restrictive.
Comparing Static and Dynamic Memory Allocation (Linked Lists)
Feature | Static Allocation | Dynamic Allocation (Linked Lists) |
---|---|---|
Allocation Time | Compile-time — but impractical for linked lists. | Runtime — essential for creating flexible, expandable node structures. |
Memory Location | Stack — limited, fixed size. | Heap — allows memory to be allocated as needed. |
Size Flexibility | Fixed — total number of nodes must be known ahead of time. | Flexible — nodes can be added/removed dynamically. |
Management | Automatic (handled by compiler). | Manual (must malloc() and free() nodes yourself). |
Use Case | Very rare or only for learning/demo. | Real-world use — dynamic nature suits linked list structure. |
Risk of Leaks | Minimal — automatically cleaned up. | High — if free() is not called for each allocated node. |
Performance | Fast due to contiguous memory. | Slightly slower — due to non-contiguous memory and heap overhead. |
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check C Books
- Apply for C Internship
- Check Computer Science Books
- Practice BCA MCQs
- Watch Advanced C Programming Videos