20-09-2026
207 lines
5.3 kB
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
15static 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
42static 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
63typedef struct {
64 char name[16];
65 int hp;
66} monster;
67
68static 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
93static 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
110static 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
133static 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
143static 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, ©)) {
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
170static 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
197int 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}