DYNARR(3pub)
π DYNARR β the 3pub girlies' guide to simple dynamic arrays π
okay so bestie, welcome to DYNARR(3pub), straight out of the C Programmer's Manual. This is the page about dynarr, dynarr_init, dynarr_resize and dynarr_free β literally just simple dynamic arrays. No cap. πΈ
β¨ NAME β¨
dynarr, dynarr_init, dynarr_resize, dynarr_free β simple dynamic arrays π¦
π SYNOPSIS π
#include <publib.h>
void dynarr_init(struct dynarr *da, size_t elsize);
int dynarr_resize(struct dynarr *da, size_t newsize);
void dynarr_free(struct dynarr *da);
π DESCRIPTION π
These functions make it easier to use dynamic arrays β and by that we mean arrays that get allocated with malloc(3) and resized with realloc(3). The way that everyone hand-rolls this is lowkey a mess, so let's look at the typical code fragment for implementing a dynamic array that gets resized as more input is read. π
char *p, *line;
size_t alloc, len;
len = 0;
alloc = 1024;
if ((line = malloc(alloc)) == NULL) abort();
while (fgets(line + len, alloc-len, stdin) != NULL) {
len = strlen(line);
alloc += 1024;
if ((p = realloc(alloc)) == NULL) abort();
alloc = p;
}
(The error handling is intentionally simplified.) Girl, they told us themselves. π
And now the same fragment but with the dynarr(3) glow-up:
struct dynarr da;
dynarr_init(&da);
while (fgets((char *)da.data + da.used, da.alloc-da.len, stdin) != NULL) {
da.used = strlen(da.data);
if (dynarr_resize(&da, da.alloc + 1024) == -1) abort();
}
It's giving cleaner code, fr fr. The code is a bit simpler, and all the memory allocation details plus most of the error checking code are hidden away. Obsessed. β¨
The dynamic array is represented by a struct dynarr:
struct dynarr {
void *data;
size_t alloc, used;
};
The interface to the dynamic allocation has intentionally been made unopaque. Yes bestie, on purpose, not a mistake. π
So here's the rundown, one function at a time:
dynarr_initβ initializes astruct dynarrto be an empty array. Fresh start, main character energy. πdynarr_resizeβ sets its size to benewsize. Glow-up arc. π¦dynarr_freeβ frees the array (it will become an empty array again). Closure, we love to see it. πΈ
π«Ά RETURNS π«Ά
dynarr_resize returns -1 if it failed, and 0 if it succeeded.
β οΈ And listen β it does not change the array in any way if it failed. So if it flops, your array is untouched. No damage, no drama. π
π SEE ALSO π
publib(3), malloc(3), realloc(3), strdup(3) β the whole friend group, iykyk. π§
π AUTHOR π
Lars Wirzenius (lars.wirzenius@helsinki.fi) β icon, legend, the moment. π
okay that's the whole page bestie, go write some arrays π
This was Publib, from the C Programmer's Manual, section DYNARR(3pub) β mwah, slay, periodt. π β¨π
π¦ where this page comes from
- debian bookworm: publib-dev 0.40-3+b1 Β· BSD-2-Clause
- debian trixie: publib-dev 0.40-4 Β· BSD-2-Clause
- ubuntu jammy: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu noble: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu questing: publib-dev 0.40-4
- ubuntu resolute: publib-dev 0.40-4build1 Β· BSD-2-Clause
- ubuntu stonking: publib-dev 0.40-4build1 Β· BSD-2-Clause
- Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms