C Notes
Complete guide to dynamic arrays in C — creating arrays at runtime, growing arrays with realloc, implementing ArrayList-like behavior, 2D dynamic arrays, and practical examples with output.
In C, regular arrays have a fixed size determined at compile time. But real-world programs often need arrays that can grow or shrink based on user input, file data, or runtime conditions. Dynamic arrays solve this problem by combining malloc(), realloc(), and free() to create flexible, resizable containers.
The Problem with Static Arrays
Creating a Dynamic Array
How many numbers do you want to store? 4 Enter 4 numbers: 10 20 30 40 Sum = 100, Average = 25.00
Growing an Array (ArrayList Pattern)
The classic pattern doubles the capacity when the array is full:
Pushing 10... Pushing 20... Pushing 30... [Grew capacity: 2 -> 4] Pushing 40... Pushing 50... [Grew capacity: 4 -> 8] Pushing 60... Pushing 70... Pushing 80... Final array: [ 10 20 30 40 50 60 70 80 ] (size=8, capacity=8)
Why Double the Capacity?
| Growth Strategy | Amortized Cost per Push | Memory Overhead |
|---|---|---|
| +1 each time | O(n) — copies every time | Minimal |
| +10 each time | O(n) — still linear | Low |
| ×2 (double) | O(1) amortized | Up to 50% wasted |
| ×1.5 | O(1) amortized | Up to 33% wasted |
Doubling gives O(1) amortized insert time. Each element is copied at most O(log n) times total.
Dynamic 2D Array
Enter rows and cols: 4 5 Multiplication table (4x5): 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20
Contiguous 2D Array (Single Allocation)
The pointer-to-pointer approach above has fragmented memory. For better cache performance, allocate one contiguous block:
Contiguous 2D array: 0 1 2 3 10 11 12 13 20 21 22 23
Removing Elements from a Dynamic Array
Before remove: 10 20 30 40 50 60 After remove: 10 20 40 50 60
Memory Layout Comparison
| Static Array | int arr[5] |
| Dynamic Array | int *arr = malloc(5 * sizeof(int)) |
| │ ptr ─────┼───── | │ [0] [1] [2] [3] [4] │ |
| Dynamic 2D | int **matrix |
| │ matrix ──┼───── | │ [0] ────┼─────→│ cols... │ |
| └──────────┘ └─────────┘│ └── | │ cols... │ |
| └──── | │ cols... │ |
Interview Questions
Q1: What is the time complexity of inserting at the end of a dynamic array?
Amortized O(1) if you use a doubling strategy. Individual insertions that trigger a resize are O(n) (due to copying), but these happen rarely enough that the average cost per operation is constant.
Q2: How do you implement a dynamic array that shrinks?
When the size drops below 1/4 of the capacity, shrink to half capacity using realloc(). This prevents thrashing (repeated grow/shrink) at the boundary. Never shrink below a minimum capacity (e.g., 4).
Q3: What's the difference between VLA (Variable Length Arrays) and dynamic arrays?
VLAs (C99) are allocated on the stack with automatic lifetime — they can't be resized and can cause stack overflow for large sizes. Dynamic arrays are on the heap, can be resized with realloc(), and persist until explicitly freed.
Q4: Why is the contiguous 2D array approach better for performance?
Contiguous memory has better cache locality — the CPU can prefetch adjacent elements. The pointer-to-pointer approach scatters rows across the heap, causing cache misses. Contiguous arrays also require only one malloc/free call.
Summary
- Dynamic arrays use
malloc()to allocate arrays whose size is determined at runtime - The doubling strategy (grow by 2×) gives O(1) amortized insertion time
- Use a struct to encapsulate
data,size, andcapacity - For 2D arrays, prefer contiguous allocation for performance-critical code
- Always free all allocated memory and handle realloc failures safely
- Dynamic arrays form the basis of many data structures (stacks, queues, hash tables)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Dynamic Arrays in C – Runtime-Sized Arrays.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, dynamic, memory, allocation, arrays, dynamic arrays in c – runtime-sized arrays
Related C Programming Topics