Created
September 3, 2025 18:55
-
-
Save RandyGaul/9360fc62bf38052a0161fb55a14613db to your computer and use it in GitHub Desktop.
dynamic array in C (stretchy buffer technique by stb)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| //-------------------------------------------------------------------------------------------------- | |
| // Dynamic array public API. | |
| #define array_count(a) ((a) ? ARRAY_HEADER(a)->len : 0) | |
| #define array_cap(a) ((a) ? ARRAY_HEADER(a)->cap : 0) | |
| #define array_free(a) ((a) ? (free(ARRAY_HEADER(a)), (a)=NULL, 0) : 0) | |
| #define array_add(a, val) (array__maybegrow(a, 1), (a)[ARRAY_HEADER(a)->len++] = (val)) | |
| //-------------------------------------------------------------------------------------------------- | |
| // Dynamic array "private" implementation. | |
| #define ARRAY_HEADER(a) ((ArrayHeader*)a - 1) | |
| typedef struct ArrayHeader | |
| { | |
| int len; | |
| int cap; | |
| } ArrayHeader; | |
| static void* array_grow_impl(void* buf, size_t element_size, size_t add) | |
| { | |
| size_t new_len = buf ? ARRAY_HEADER(buf)->len + add : add; | |
| size_t new_cap = buf ? ARRAY_HEADER(buf)->cap * 2 : 16; | |
| if (new_cap < new_len) new_cap = new_len; | |
| size_t size = sizeof(ArrayHeader) + new_cap * element_size; | |
| ArrayHeader* new_hdr; | |
| if (buf) { | |
| new_hdr = (ArrayHeader*)realloc(ARRAY_HEADER(buf), size); | |
| } else { | |
| new_hdr = (ArrayHeader*)malloc(size); | |
| new_hdr->len = 0; | |
| } | |
| new_hdr->cap = (int)new_cap; | |
| return (void*)(new_hdr + 1); | |
| } | |
| #define array__needgrow(a, n) ((a)==NULL || ARRAY_HEADER(a)->len + (n) > ARRAY_HEADER(a)->cap) | |
| #define array__maybegrow(a, n) (array__needgrow(a, (n)) ? (*(void**)&(a)=array_grow_impl(a, sizeof(*(a)), n)) : 0) | |
| //-------------------------------------------------------------------------------------------------- | |
| // Demo. | |
| int main() | |
| { | |
| int* a = NULL; | |
| for (int i = 0; i < 100; ++i) { | |
| array_add(a, i); | |
| } | |
| for (int i = 0; i < 100; ++i) { | |
| printf("%d\n", a[i]); | |
| } | |
| printf("len=%d cap=%d\n", array_count(a), array_cap(a)); | |
| array_free(a); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment