C Static vs Dynamic Structures

Intermediate
6 min read
Updated: Nov 2026
1 example

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.

Remember
int fixed[4];                       /* static capacity */
int *grow = malloc(2 * sizeof *grow);  /* dynamic */
grow = realloc(grow, 4 * sizeof *grow);

Cheat Sheet

Keep these facts handy while you work with Static vs Dynamic Structures in C.

TopicDetail
StaticFixed size, automatic storage
Dynamicmalloc, realloc, free
GrowthUsually double the capacity
RiskLeaks and dangling pointers

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Abstract Data Types.

Example 1 — A growing array

Double the capacity when the array is full.

C
#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;
}

How It Works

  1. The static array never changes size.
  2. Capacity doubles 2 to 4 at the third item and 4 to 8 at the fifth.
  3. 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.

Key Takeaways

  • Static: simple, fixed.
  • Dynamic: flexible, manual.
  • Grow geometrically.

One line: Static structures have a size fixed at compile time, such as int a[100].

Frequently Asked Questions

Static structures have a fixed size decided at compile time; dynamic structures change size at run time using heap allocation.
Dynamic: nodes are allocated and freed individually as the list changes.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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