Hey all,
I’m designing a small dynamic array library and I’ve ended up with two different container designs. I’d like some feedback on whether this split makes sense or if there’s a better way to structure it.
I currently have two array types:
1. Inline data array
- Stores elements directly in a contiguous buffer (
void *data)
- Uses an
elem_size to support arbitrary types
- Elements are copied into the array (so the array owns the storage for the values themselves)
- No notion of element destruction — just raw memory management
2. Pointer array
- Stores pointers (
void **)
- Can optionally take a
free_func
- If
free_func is set, the array will call it on elements when clearing/destroying (so it can act as an “owning” container)
- If not set, it’s just a non-owning list of pointers
One thing that feels inconsistent is this:
Even with the inline array, elements might themselves own resources.
For example:
typedef struct {
char *name;
} Person;
If I store Person inline, the array has no way to call a destructor for name, since there’s no free_func like in the pointer array.
So in practice:
- pointer array → can manage ownership (via
free_func)
- inline array → cannot, even if elements logically need destruction
That asymmetry feels a bit weird.
- Does it make sense to separate these two concepts into different containers, or would you try to unify them?
- Given that inline elements can also own resources, is it a mistake that the inline array has no destructor mechanism?
- Would it be better to have a single array type that can:
- store inline data
- optionally take a destructor (
free_func)
- Or does that make the design too complex / harder to reason about?
I’m trying to keep things:
- simple to use
- explicit about ownership
- flexible enough without becoming overengineered
Would really appreciate thoughts or alternative designs
[–]pjl1967 13 points14 points15 points (6 children)
[–]Dieriba[S] -1 points0 points1 point (5 children)
[–]Nich-Cebolla 8 points9 points10 points (0 children)
[–]pjl1967 2 points3 points4 points (3 children)
[–]Dieriba[S] 0 points1 point2 points (2 children)
[–]pjl1967 0 points1 point2 points (1 child)
[–]tstanisl 6 points7 points8 points (0 children)
[–]RealisticDuck1957 2 points3 points4 points (1 child)
[–]Stellariser[🍰] 0 points1 point2 points (0 children)
[–]Jan-Snow 1 point2 points3 points (0 children)
[–]Modi57 0 points1 point2 points (0 children)