4 files changed, +317, -0
cc8ba88Initial commit
author
mogi <git@paramogi.world> on 20-09-2026 22:08 UTC+1
committer
mogi <git@paramogi.world> on 20-09-2026 22:08 UTC+1
LICENSE
+18, -0
README.md
+19, -0
examples.c
+207, -0
kudzu.h
+73, -0
+18, -0
    @@ -0,0 +1,18 @@
   1+MIT License
   2+
   3+Copyright (c) 2026 paramogi
   4+
   5+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
   6+associated documentation files (the "Software"), to deal in the Software without restriction, including
   7+without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8+copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
   9+following conditions:
  10+
  11+The above copyright notice and this permission notice shall be included in all copies or substantial
  12+portions of the Software.
  13+
  14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  15+LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
  16+EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18+USE OR OTHER DEALINGS IN THE SOFTWARE.
+19, -0
    @@ -0,0 +1,19 @@
   1+# kudzu
   2+
   3+Tiny dynamic array header, mostly meant for me.
   4+
   5+Supports C99. Only tested on amd64. Only depends on libc.
   6+
   7+## Usage
   8+
   9+```
  10+#define KUDZU_IMPLEMENTATION
  11+#include "kudzu.h"
  12+```
  13+
  14+See examples.c for the rest.
  15+
  16+## Trivia
  17+
  18+Q: Why such an edgy name like "kudzu"? WTH does that even mean? \
  19+A: I came across the name in an [article](https://vickiboykis.com/2026/09/01/bad-code-is-kudzu/) and thought it was cool. It fits on two levels: dynamic arrays grow like kudzu does, and the author argues that bad code is like kudzu -- this is pretty bad code, so it is pretty much like kudzu.
+207, -0
      @@ -0,0 +1,207 @@
     1+/*
     2+ * example.c - usage examples for kudzu.h
     3+ *
     4+ * Build:
     5+ * cc -std=c99 -Wall -Wextra -o examples examples.c
     6+ */
     7+
     8+#define KUDZU_IMPLEMENTATION
     9+#include "kudzu.h"
    10+
    11+#include <stdio.h>
    12+#include <string.h>
    13+
    14+// ints
    15+static void example_int(void)
    16+{
    17+    // 0 capacity is fine, the first darray_append allocates
    18+    darray a = darray_init(sizeof(int), 0);
    19+    size_t i;
    20+
    21+    for (i = 0; i < 5; i++) {
    22+        int v = (int)(i * i);
    23+        // darray_append copies the value, so v can go out of scope after this
    24+        if (!darray_append(&a, &v)) {
    25+            fprintf(stderr, "out of memory\n");
    26+            darray_free(&a);
    27+            return;
    28+        }
    29+    }
    30+
    31+    printf("ints:");
    32+    for (i = 0; i < a.len; i++)
    33+        // clunky asf, i know.
    34+        // however, this was the only way i found of doing it
    35+        printf(" %d", *(int *)darray_at(&a, i));
    36+    printf(" (len=%zu, cap=%zu)\n", a.len, a.cap);
    37+
    38+    darray_free(&a);
    39+}
    40+
    41+// doubles
    42+static void example_double(void)
    43+{
    44+    // preallocating avoids the regrow
    45+    darray d = darray_init(sizeof(double), 4);
    46+    double sum = 0.0;
    47+    size_t i;
    48+
    49+    darray_append(&d, &(double){ 3.14 });
    50+    darray_append(&d, &(double){ 2.71 });
    51+    darray_append(&d, &(double){ 1.41 });
    52+
    53+    for (i = 0; i < d.len; i++)
    54+        // again, clunky asf... sorry to whoever is reading this
    55+        sum += *(double *)darray_at(&d, i);
    56+
    57+    printf("doubles: sum=%.2f (len=%zu, cap=%zu)\n", sum, d.len, d.cap);
    58+
    59+    darray_free(&d);
    60+}
    61+
    62+// structs
    63+typedef struct {
    64+    char    name[16];
    65+    int     hp;
    66+} monster;
    67+
    68+static void example_struct(void)
    69+{
    70+    darray m = darray_init(sizeof(monster), 2);
    71+    monster *p;
    72+    size_t i;
    73+
    74+    darray_append(&m, &(monster){ "vine", 30 });
    75+    darray_append(&m, &(monster){ "creeper", 55 });
    76+    darray_append(&m, &(monster){ "kudzu", 100 });
    77+
    78+    // darray_append returns a pointer to the stored copy, so we can edit it
    79+    p = darray_append(&m, &(monster){ "sprout", 0 });
    80+    if (p)
    81+        p->hp = 5;
    82+
    83+    printf("monsters:\n");
    84+    for (i = 0; i < m.len; i++) {
    85+        monster *mp = darray_at(&m, i);
    86+        printf(" %s hp=%d\n", mp->name, mp->hp);
    87+    }
    88+
    89+    darray_free(&m);
    90+}
    91+
    92+// strings
    93+static void example_string_buffer(void)
    94+{
    95+    darray s = darray_init(sizeof(char), 0);
    96+    const char *msg = "bad code is kudzu";
    97+    size_t i;
    98+
    99+    for (i = 0; msg[i]; i++)
   100+        darray_append(&s, &msg[i]);
   101+    darray_append(&s, &(char){ '\0' });
   102+
   103+    // items is contiguous, so it can be handed to any char* function
   104+    printf("string: %s (len=%zu including NUL, cap=%zu)\n", (char *)s.items, s.len, s.cap);
   105+
   106+    darray_free(&s);
   107+}
   108+
   109+// multiple strings
   110+static void example_string_list(void)
   111+{
   112+    darray list = darray_init(sizeof(char *), 0);
   113+    const char *third = "goodbye";
   114+    size_t i;
   115+
   116+    // the element IS a pointer, so we pass the address of the pointer
   117+    darray_append(&list, &(const char *){ "hello" });
   118+    darray_append(&list, &(const char *){ "world" });
   119+    darray_append(&list, &third);
   120+
   121+    printf("multiple strings:");
   122+    for (i = 0; i < list.len; i++) {
   123+        // darray_at gives a char**, hence the extra deref
   124+        printf(" %s", *(const char **)darray_at(&list, i));
   125+    }
   126+    printf("\n");
   127+
   128+    // only need to free the items list, since every other string is on the stack
   129+    darray_free(&list);
   130+}
   131+
   132+// malloc'd strings
   133+static char *dup_str(const char *s)
   134+{
   135+    size_t n = strlen(s) + 1;
   136+    char *p = malloc(n);
   137+
   138+    if (p)
   139+        memcpy(p, s, n);
   140+    return p;
   141+}
   142+
   143+static void example_owned_strings(void)
   144+{
   145+    darray list = darray_init(sizeof(char *), 0);
   146+    const char *words[] = { "prune", "the", "vines" };
   147+    size_t i;
   148+
   149+    for (i = 0; i < sizeof words / sizeof *words; i++) {
   150+        char *copy = dup_str(words[i]);
   151+        if (!copy || !darray_append(&list, &copy)) {
   152+            free(copy);
   153+            break;
   154+        }
   155+    }
   156+
   157+    printf("malloc'd:");
   158+    for (i = 0; i < list.len; i++)
   159+        printf(" %s", *(char **)darray_at(&list, i));
   160+    printf("\n");
   161+
   162+    // darray_free never touches what the elements point to
   163+    // so we need to free each one individually
   164+    for (i = 0; i < list.len; i++)
   165+        free(*(char **)darray_at(&list, i));
   166+    darray_free(&list);
   167+}
   168+
   169+// bounds and pointer invalidation
   170+static void example_gotchas(void)
   171+{
   172+    darray a = darray_init(sizeof(int), 1);
   173+    int *first;
   174+    size_t i;
   175+
   176+    darray_append(&a, &(int){ 1 });
   177+
   178+    // out of range gives NULL
   179+    printf("at(99) = %p\n", (void *)darray_at(&a, 99));
   180+
   181+    // a pointer into the array stays valid only until the next regrow
   182+    first = darray_at(&a, 0);
   183+    for (i = 0; i < 64; i++)
   184+        darray_append(&a, &(int){ 0 });
   185+    // 'first' is dangling now, since realloc may have moved the block.
   186+    // we need to re-fetch after any append that can grow
   187+    first = darray_at(&a, 0);
   188+    printf("first = %d (len=%zu, cap=%zu)\n", *first, a.len, a.cap);
   189+
   190+    // darray_free leaves the struct reusable
   191+    darray_free(&a);
   192+    darray_append(&a, &(int){ 7 });
   193+    printf("reused after free: %d\n", *(int *)darray_at(&a, 0));
   194+    darray_free(&a);
   195+}
   196+
   197+int main(void)
   198+{
   199+    example_int();
   200+    example_double();
   201+    example_struct();
   202+    example_string_buffer();
   203+    example_string_list();
   204+    example_owned_strings();
   205+    example_gotchas();
   206+    return 0;
   207+}
+73, -0
    @@ -0,0 +1,73 @@
   1+#ifndef KUDZU_H
   2+#define KUDZU_H
   3+
   4+#include <stddef.h>
   5+
   6+typedef struct {
   7+    void    *items;
   8+    size_t  elem_size;
   9+    size_t  len;
  10+    size_t  cap;
  11+} darray;
  12+
  13+darray darray_init(size_t size, size_t initial_capacity);
  14+void *darray_append(darray *arr, const void *elem);
  15+void *darray_at(darray *arr, size_t index);
  16+void darray_free(darray *arr);
  17+
  18+#endif
  19+
  20+#ifdef KUDZU_IMPLEMENTATION
  21+
  22+#include <stdint.h>
  23+#include <stdlib.h>
  24+#include <string.h>
  25+
  26+#include <stdio.h>
  27+
  28+darray darray_init(size_t elem_size, size_t initial_cap) {
  29+    darray arr = { NULL, elem_size, 0, 0 };
  30+
  31+    if (initial_cap && elem_size <= SIZE_MAX / initial_cap) {
  32+        arr.items = malloc(initial_cap * elem_size);
  33+        if (arr.items)
  34+            arr.cap = initial_cap;
  35+    }
  36+    return arr;
  37+}
  38+
  39+void *darray_append(darray *arr, const void *elem) {
  40+    void *slot;
  41+
  42+    if (arr->len == arr->cap) {
  43+        size_t new_cap = arr->cap ? arr->cap * 2 : 16;
  44+        void *p;
  45+
  46+        if (new_cap && arr->elem_size > SIZE_MAX / new_cap)
  47+            return NULL;
  48+        p = realloc(arr->items, new_cap * arr->elem_size);
  49+        if (!p)
  50+            return NULL;
  51+        arr->items = p;
  52+        arr->cap = new_cap;
  53+    }
  54+
  55+    slot = (char *)arr->items + arr->len * arr->elem_size;
  56+    memcpy(slot, elem, arr->elem_size);
  57+    arr->len++;
  58+    return slot;
  59+}
  60+
  61+void *darray_at(darray *arr, size_t index) {
  62+    if (index >= arr->len)
  63+        return NULL;
  64+    return (char *)arr->items + index * arr->elem_size;
  65+}
  66+
  67+void darray_free(darray *arr) {
  68+    free(arr->items);
  69+    arr->items = NULL;
  70+    arr->len = arr->cap = 0;
  71+}
  72+
  73+#endif