r/C_Programming • u/Elifire12 • Sep 08 '26
Article Generic Dynamic Arrays in C
https://eliasebner.com/blog/guides/generic-dynamic-arrays-in-c/After implementing strings , I implemented dynamic arrays in C and wrote an article about it. The implementation is generic, I talk about the trade-offs of this approach in the article.
If you only care about the code, it's here.
Tell me what you think!
0
Upvotes
•
u/AutoModerator Sep 08 '26
Hi /u/Elifire12,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
16
u/FirmAndSquishyTomato Sep 09 '26
If you're going to claim this is utility code that others can use in their projects, you think you'd put some guards in place. Checking the return from your realloc calls would be a good place to start. The weird inefficient need to copy memory, taking raw pointers when the code assumes it's always of a specific type
Your article is written in a way that you're suggesting that you are an authority on this subject, but the code itself shows that is not the case.
5
u/musbur Sep 09 '26
In C, you have a choice: Either write some per-project helper functions to implement some specific data structures you need. Or use a proven, well-known generic library that covers tons of cases, has 10x more SLOC than your own code, and requires tons of biolerplate. Pick your poison.
I saw your article ranting about C's string implmentation, and just the first assumptions were so wrong that I didn't bother to read the rest.
4
u/sciencekm Sep 09 '26
As for this:
I'm curious as to why you are using a void* instead of the array structure as the input, and why do you need to make a temporary copy of the structure.
Would something like this not suffice?
2
u/SmokeMuch7356 Sep 09 '26
I have concerns:
Are you ever going to apply
da_pushto something that isn't astruct da_array? Are you expecting any user of your library to do so? You are assuming that whateverdapoints to will always map cleanly onto that structure, so why not makedaastruct da_array *to begin with? Same question the other two functions.What purpose does
replicaactually serve? You're not doing any validation or error checking before copying back toda, so why bother creating it in the first place?You should verify that the
realloccall succeeded before updating.capor.data:While I understand what
is doing, this is one of those cases where terseness isn't a virtue; it would be clearer if you broke the update to
.lenout into a separate statement:2
u/flewanderbreeze Sep 09 '26
> Generic DS Implementation in C
> Looks inside
> Void pointers
:(
1
u/flyingron Sep 09 '26
You do know that since 1977 or so it has been possible to assign structs in C. You don't have to copy them with memcpy all the time.