Skip to content

Instantly share code, notes, and snippets.

@riturajborpujari
Created December 23, 2025 04:12
Show Gist options
  • Select an option

  • Save riturajborpujari/c9b0a762617b3c58c13f832634b9c243 to your computer and use it in GitHub Desktop.

Select an option

Save riturajborpujari/c9b0a762617b3c58c13f832634b9c243 to your computer and use it in GitHub Desktop.
Dynamic Array in C
#ifndef __D_ARRAY_H__
#define __D_ARRAY_H__
#include <stdlib.h>
#define da_init(name, type) \
typedef struct {\
type *items;\
size_t count;\
size_t capacity;\
} name;\
#define da_append(xs, x) \
do {\
if (xs.capacity == xs.count) {\
if (xs.capacity == 0) xs.capacity = 128;\
else xs.capacity *= 2;\
xs.items = realloc(xs.items, xs.capacity * sizeof(xs.items));\
}\
xs.items[xs.count++] = x;\
} while (0);
#define da_free(xs) \
do {\
free(xs.items);\
} while(0);
#endif /* __D_ARRAY_H__ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment