|
| 1 | +--- |
| 2 | +title: "Build a Dynamic Array in C From Scratch" |
| 3 | +description: "How to Build a Dynamic Array in C" |
| 4 | +pubDate: "September 12 2025" |
| 5 | +updatedDate: "September 12 2025" |
| 6 | +tags: ["C", "Data Structures", "Tutorial"] |
| 7 | +featured: true |
| 8 | +--- |
| 9 | + |
| 10 | +Dynamic arrays are a fundamental data structure in computer science. Unlike static arrays, they automatically resize themselves to accommodate new elements. |
| 11 | +In this post, we will build a minimal dynamic array in C from scratch with no external libraries. |
| 12 | + |
| 13 | +## Lets Get Started |
| 14 | + |
| 15 | +We will start with creating three files: |
| 16 | + |
| 17 | +```sh |
| 18 | +touch DynamicArray.h DynamicArray.c main.c |
| 19 | +``` |
| 20 | + |
| 21 | +- `DynamicArray.h` - API declarations will go here |
| 22 | +- `DynamicArray.c` - Implementation will go here |
| 23 | +- `main.c` - demo and testing will be done here |
| 24 | + |
| 25 | +## API |
| 26 | + |
| 27 | +```c |
| 28 | +// DynamicArray.h |
| 29 | +typedef struct { |
| 30 | + int *buffer; |
| 31 | + size_t size; |
| 32 | + size_t capacity; |
| 33 | +} DynamicArray; |
| 34 | + |
| 35 | +extern void DynamicArrayInit(DynamicArray *array, int size); |
| 36 | +extern void DynamicArrayPushBack(DynamicArray *array, int item); |
| 37 | +extern void DynamicArrayPrint(DynamicArray *array); |
| 38 | +extern void DynamicArrayPopBack(DynamicArray *array); |
| 39 | +extern void DynamicArrayCleanup(DynamicArray *array); |
| 40 | +``` |
| 41 | +
|
| 42 | +## Implementation |
| 43 | +
|
| 44 | +### Initialization |
| 45 | +
|
| 46 | +`extern void DynamicArrayInit(DynamicArray *array, int size);` - initializes the array and allocates memory |
| 47 | +
|
| 48 | +```c |
| 49 | +// DynamicArray.c |
| 50 | +
|
| 51 | +#include "DynamicArray.h" |
| 52 | +
|
| 53 | +void DynamicArrayInit(DynamicArray *array, int size) { |
| 54 | + if (size <= 0) { size = 1; } |
| 55 | + array->size = 0; |
| 56 | + array->capacity = size * 2; |
| 57 | + array->buffer = malloc(sizeof *array->buffer * array->capacity); |
| 58 | + memset(array->buffer, 0, sizeof *array->buffer * array->capacity); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Adding Elements |
| 63 | + |
| 64 | +`extern void DynamicArrayPushBack(DynamicArray *array, int item);` - appends elements and expands the capacity if needed |
| 65 | + |
| 66 | +```c |
| 67 | +void DynamicArrayPushBack(DynamicArray *array, int item) { |
| 68 | + if (array->size >= array->capacity) { |
| 69 | + array->capacity *= 2; |
| 70 | + array->buffer = realloc(array->buffer, |
| 71 | + sizeof *array->buffer * array->capacity); |
| 72 | + } |
| 73 | + array->buffer[array->size++] = item; |
| 74 | +} |
| 75 | +``` |
| 76 | +
|
| 77 | +### Removing Elements |
| 78 | +
|
| 79 | +`extern void DynamicArrayPopBack(DynamicArray *array);` - removes the last element from the array, does nothing if array is empty |
| 80 | +
|
| 81 | +```c |
| 82 | +
|
| 83 | +void DynamicArrayPopBack(DynamicArray *array) { |
| 84 | + if (!(array->size > 0)) { return; } |
| 85 | + array->size--; |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Printing Elements |
| 90 | + |
| 91 | +`extern void DynamicArrayPrint(DynamicArray *array);` - prints all the elements in the array, does nothing if array is empty |
| 92 | + |
| 93 | +```c |
| 94 | +void DynamicArrayPrint(DynamicArray *array) { |
| 95 | + for (int i = 0; i < array->size; i++) { |
| 96 | + printf("%d ", array->buffer[i]); |
| 97 | + } |
| 98 | + putchar('\n'); |
| 99 | +} |
| 100 | +``` |
| 101 | +
|
| 102 | +### Clean Up |
| 103 | +
|
| 104 | +`extern void DynamicArrayCleanup(DynamicArray *array);` - frees the memory |
| 105 | +
|
| 106 | +```c |
| 107 | +
|
| 108 | +void DynamicArrayCleanup(DynamicArray *array) { |
| 109 | + if (!array->buffer) { return; } |
| 110 | + array->size = array->capacity = 0; |
| 111 | + free(array->buffer); |
| 112 | + array->buffer = NULL; |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### Helpers |
| 117 | + |
| 118 | +```c |
| 119 | +// DynamicArray.c |
| 120 | +#define ALLOC_CHECK( ptr, size ) \ |
| 121 | + alloc_check( ptr, size, __FILE__, __LINE__, __func__ ) |
| 122 | +#define REALLOCATE( ptr, size ) reallocate( ptr, size ) |
| 123 | + |
| 124 | +static void alloc_check( void *ptr, const size_t size, const char *file, |
| 125 | + const int line, const char *func ); |
| 126 | +static int *reallocate( void *ptr, const size_t size ); |
| 127 | + |
| 128 | +static int *reallocate( void *ptr, const size_t size ) { |
| 129 | + int *tmp = realloc( ptr, size ); |
| 130 | + ALLOC_CHECK( ptr, size ); |
| 131 | + ptr = tmp; |
| 132 | + return ptr; |
| 133 | +} |
| 134 | + |
| 135 | +static void alloc_check( void *ptr, const size_t size, const char *file, |
| 136 | + const int line, const char *func ) { |
| 137 | + if ( !ptr ) { |
| 138 | + fprintf( stderr, |
| 139 | + "[%s:%u:(%s)] Memory allocation error. Failed to allocate %lu " |
| 140 | + "bytes to memory address %p.\n", |
| 141 | + file, line, func, size, (void *)ptr ); |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | +
|
| 146 | +- `static void alloc_check( void *ptr, const size_t size, const char *file, |
| 147 | +const int line, const char *func );` - will check to see if memory was allocated successfully |
| 148 | +- `static int *reallocate( void *ptr, const size_t size );` - expands the capacity |
| 149 | +- `ALLOC_CHECK( ptr, size )` - wrapper around `alloc_check` |
| 150 | +- `REALLOCATE( ptr, size )` - wrapper around `reallocate` |
| 151 | +
|
| 152 | +### Updated Functions |
| 153 | +
|
| 154 | +```c |
| 155 | +// DynamicArray.c |
| 156 | +
|
| 157 | +void DynamicArrayInit( DynamicArray *array, int size ) { |
| 158 | + if ( size <= 0 ) { size = 1; } |
| 159 | + array->size = 0; |
| 160 | + array->capacity = size * 2; |
| 161 | + array->buffer = malloc( sizeof *array->buffer * array->capacity ); |
| 162 | + ALLOC_CHECK( array->buffer, array->capacity ); |
| 163 | + memset( array->buffer, 0, sizeof *array->buffer * array->capacity ); |
| 164 | +} |
| 165 | +
|
| 166 | +
|
| 167 | +void DynamicArrayPushBack( DynamicArray *array, int item ) { |
| 168 | + if ( array->size >= array->capacity ) { |
| 169 | + array->capacity *= 2; |
| 170 | + array->buffer = REALLOCATE( array->buffer, |
| 171 | + sizeof *array->buffer * array->capacity ); |
| 172 | + } |
| 173 | + array->buffer[array->size++] = item; |
| 174 | +} |
| 175 | +
|
| 176 | +
|
| 177 | +``` |
| 178 | + |
| 179 | +## Example Usage |
| 180 | + |
| 181 | +```c |
| 182 | +// main.c |
| 183 | + |
| 184 | +#include "DynamicArray.h" |
| 185 | + |
| 186 | +int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv) { |
| 187 | + DynamicArray arr; |
| 188 | + DynamicArrayInit(&arr, 4); |
| 189 | + |
| 190 | + for (int i = 0; i < 10; i++) { |
| 191 | + DynamicArrayPushBack(&arr, i+1); |
| 192 | + } |
| 193 | + |
| 194 | + DynamicArrayPrint(&arr); // Output: 1 2 3 4 5 6 7 8 9 10 |
| 195 | + DynamicArrayPopBack(&arr); |
| 196 | + DynamicArrayPrint(&arr); // Output: 1 2 3 4 5 6 7 8 9 |
| 197 | + |
| 198 | + DynamicArrayCleanup(&arr); |
| 199 | + return 0; |
| 200 | +} |
| 201 | +``` |
| 202 | +
|
| 203 | +## Compile and Run |
| 204 | +
|
| 205 | +```sh |
| 206 | +gcc DynamicArray.c main.c -o dynamic_array |
| 207 | +./dynamic_array |
| 208 | +``` |
| 209 | + |
| 210 | +## Code |
| 211 | + |
| 212 | +The most updated version of the code can be found in the GitHub repository at |
| 213 | +<a href="https://github.com/ragibasif/DynamicArray.c" target="_blank" rel="noopener noreferrer">DynamicArray.c</a>. |
0 commit comments