Home C Data Structures Static vs Dynamic Structures C Static vs Dynamic Structures Definition
Fixed Size or Growing Static structures have a size fixed at compile time, such as int a[100]. They are simple and fast but waste space or overflow. Dynamic structures allocate memory at run time with malloc/realloc and can grow or shrink, at the cost of bookkeeping and the risk of leaks.
int fixed[4]; /* static capacity */
int *grow = malloc(2 * sizeof *grow); /* dynamic */
grow = realloc(grow, 4 * sizeof *grow); Reference
Cheat Sheet Keep these facts handy while you work with Static vs Dynamic Structures in C.
Topic Detail Static Fixed size, automatic storage Dynamic malloc, realloc, freeGrowth Usually double the capacity Risk Leaks and dangling pointers
Code
C Examples Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Abstract Data Types .
1. A growing array Example 1 — A growing array Double the capacity when the array is full.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int fixed[4];
int cap = 2, size = 0;
int *a = malloc(cap * sizeof *a);
if (!a) return 1;
for (int v = 1; v <= 5; v++) {
if (size == cap) {
cap *= 2;
int *t = realloc(a, cap * sizeof *a);
if (!t) { free(a); return 1; }
a = t;
}
a[size++] = v;
}
printf("static capacity: %d\n", (int)(sizeof fixed / sizeof fixed[0]));
printf("size=%d capacity=%d\n", size, cap);
free(a);
return 0;
} static capacity: 4
size=5 capacity=8 How It Works The static array never changes size. Capacity doubles 2 to 4 at the third item and 4 to 8 at the fifth. realloc result is stored in a temporary first, so a is not lost on failure. Edge Cases & Pitfalls Check these before relying on Static vs Dynamic Structures in real programs.
lose Overwriting the pointer with realloc a = realloc(a, n) leaks the old block if realloc fails.
small Growing by one Growing by a constant amount makes appends O(n). Double instead.
Remember
Key Takeaways One line: Static structures have a size fixed at compile time, such as int a[100].
Frequently Asked Questions What is the difference between static and dynamic data structures? Static structures have a fixed size decided at compile time; dynamic structures change size at run time using heap allocation.
Is a linked list static or dynamic? Dynamic: nodes are allocated and freed individually as the list changes.
What should I learn after Static vs Dynamic Structures? Continue with "C Abstract Data Types" next. Separate what a structure does from how it is built. The C Data Structures hub lists every topic in order.
Next: Abstract Data Types Separate what a structure does from how it is built.
Continue → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion