array_allocate(3)
array_allocate(3) π
okay bestie, welcome to the libowfat manual, Library Functions Manual section, and today's main character is array_allocate β she makes sure your array has at least n elements allocated. no cap. let's get into it β¨
β¨ NAME β¨
array_allocate β make sure array has at least n elements allocated π«Ά
π SYNTAX π
#include <libowfat/array.h>
void* array_allocate(array* x, uint64 membersize, int64 pos);
and like, here's how she shows up in the wild:
array x;
int64 pos;
t* p = array_allocate(&x,sizeof(t),pos);
π DESCRIPTION π
so array_allocate makes absolutely sure that enough bytes are allocated in x for at least pos+1 objects of type t. π¦
β οΈ the size of t must be positive β otherwise the effects are undefined. not "kinda weird", literally undefined. do not test her.
if not enough bytes are allocated (or x is unallocated), array_allocate allocates more bytes, moving the dynamically allocated region if necessary. and she's generous about it: array_allocate often allocates somewhat more bytes than necessary, to save time later. foresight? iconic. π
then array_allocate makes sure the number of bytes initialized covers at least those pos+1 objects. if not enough bytes are initialized, she initializes more bytes β setting them to 0 β up to exactly the end of the pos+1st object.
after all that, array_allocate returns a pointer to the pos+1st object; i.e., object number pos, with objects numbered starting at 0. this pointer can be used to change or inspect the object. π«
the pointer can keep being used through subsequent calls to array_get, array_start, array_length, and array_bytes β but it must not be used after any other operations on this array. it's giving temporary VIP pass, not a forever key. π
if something goes wrong, array_allocate returns 0, setting errno appropriately, without touching x. in particular, array_allocate returns 0 if:
xhas failed, orposis negative, or- not enough memory is available.
and important: array_allocate does not change x to have failed. if you want that, use array_fail. she's not gonna put that on your array without your say-so, bestie. πΈ
π₯ PERFORMANCE π₯
this function can call realloc when the array needs to be enlarged. under exceptional circumstances, this can lead to blocking the current thread. π
it will also zero-fill the newly enlarged part of the array, which leads to all pages being mapped in by the operating system. and if a small array gets enlarged to a very large array, this can lead to swapping and blocking. π so like, yes she's convenient, but she's not free.
π SEE ALSO π
and that's the whole page, fr fr β array_allocate(3) from the Library Functions Manual, out here serving you the libowfat array lore. go forth and allocate responsibly, bestie ππβ
π¦ where this page comes from
- alpine 3.24: libowfat-doc 0.34-r0 Β· GPL-2.0-only
- debian bookworm: libowfat-dev 0.32-4 Β· GPL-2.0-only
- debian trixie: libowfat-dev 0.32-5 Β· GPL-2.0-only
- nixos 26.05: libowfat 0.34
- nixpkgs unstable: libowfat 0.34
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- ubuntu noble: libowfat-dev 0.32-4.1build1 Β· GPL-2.0-only
- ubuntu questing: libowfat-dev 0.32-5
- ubuntu resolute: libowfat-dev 0.34-2 Β· GPL-2.0-only
- ubuntu stonking: libowfat-dev 0.34-2 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms