array(3)

πŸ“¦
version 0.34 Β· 6 versions
, in alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– array(3) β€” the array library interface πŸ’–

okay bestie, welcome to the array(3) page, straight outta the Library Functions Manual ✨ this is the whole array library interface and honestly? it's giving organized. it's giving memory-safe girlboss. let's go 🫢


🌸 NAME 🌸

array β€” The array library interface πŸ’…


✨ SYNTAX ✨

#include <libowfat/array.h>

like literally just include it and you're in, no cap πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so an allocated array variable is keeping track of three things at all times, and she's tracking them obsessively:

  • a (nonzero) pointer to a dynamically allocated region of memory πŸ’Ύ
  • the number of bytes allocated (always positive, periodt) πŸ“
  • and the number of bytes initialized (which lives somewhere between 0 and the number of bytes allocated) βœ…

there are two other possibilities for the state of an array variable, and they're the sad ones: unallocated and failed 😭 in both of those cases there is no dynamically allocated region of memory. none. zero. the void.

a new array variable is normally created as a static variable, like so:

#include <libowfat/array.h>

static array x;

at this point it's unallocated β€” just a baby, no memory yet πŸ™ˆ the array library provides various allocation and inspection functions to fix that.

a new array variable can also be created dynamically πŸŒ€ but listen, this is important: it must be initialized to all-0, meaning unallocated, before it is given to any of the array functions. and it must be returned to the unallocated (or failed) state β€” for example with array_reset β€” before it is destroyed. these rules prevent all memory leaks 🧚 and memory leaks are not the vibe.

πŸ” Expansion and inspection πŸ”

array x;

t* p1 = array_allocate(&x,sizeof(t),pos);

t* p2 = array_get(&x,sizeof(t),pos);

t* p3 = array_start(&x);

int64 len = array_length(&x,sizeof(t));

int64 bytes = array_bytes(&x);

βœ‚οΈ Truncation and deallocation βœ‚οΈ

array x;

array_truncate(&x,sizeof(t),len);

array_trunc(&x);

array_reset(&x);

array_fail(&x);

βš–οΈ Comparison βš–οΈ

array x;
array y;

if (array_equal(&x,&y))
  /* arrays are equal... */

🧩 Concatenation 🧩

array x;
array y;

array_cat(&x,&y);

array_catb(&x,"fnord",5);

array_cats(&x,"fnord");

array_cats0(&x,"fnord"); /* also append the \0 */

array_cat0(&x); /* append \0 */

array_cate(&x,"fnord",1,4); /* append "nor" */

πŸ“œ ORIGINAL API DEFINITION πŸ“œ

http://cr.yp.to/lib/array.html


πŸ‘‘ SEE ALSO πŸ‘‘

array_get(3), array_start(3), array_fail(3)


and that's the whole page bestie πŸ’– this was array(3) from the Library Functions Manual β€” go forth and allocate responsibly, return everything to unallocated before it's destroyed, and remember: no leaks, only slay πŸ’…βœ¨πŸŽ€πŸ¦‹