r/C_Programming • • 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

16 comments sorted by

View all comments

Show parent comment

1

u/Certain-Flow-0 May 22 '26

Last nitpick:

array_reserve has different results if invoked with different parameters:

capacity = 32, len = 27 (free space = 5)

Calling reserve twice with res_len = 5, doesnt do a reallocation. But one reserve with res_len = 10 does.

This is not an issue if it's a private function in the library but it can be problematic if it's public

3

u/pjl1967 May 22 '26

If you reserve space for 5 additional elements then proceed not to actually add any elements, then reserve space for 5 additional elements again, it correctly does nothing because there already is space for 5 additional elements.

I don't see this as manifesting any issue at all. It's working as designed.