r/C_Programming • u/pjl1967 • May 20 '26
My own bare-bones dynamic array
About a month ago, somebody posted asking for design advice for a dynamic array. My then advice was to treat the element type as an opaque type T.
I've had my own implementation of such a dynamic array lying around for a while, but finally had a use for it, so I gave it a bit of polish and it's here:
If you wanted it to be even more bare-bones, you could keep only the regular (bounds-checking) functions or the no-check (_nc) functions, whichever you prefer.
10
Upvotes
View all comments
Show parent comment
1
u/WittyStick May 20 '26 edited May 20 '26
You would also use
bit_ceilin array_reserve to ensure it's always a power of 2.If your array cap is not always a power of 2 then indeed you should store it.
Yeah, popcount is C23 (
stdc_count_ones), but there are implementations that work for prior versions. GCC has __buitltn_popcountll, <immintrin.h> exposes _popc_u64 etc.Most modern CPUs support popcount, clz etc, but some low powered CPUs may not have and would need a slower software implementation.