Live data from Hacker News

Generic dynamic array in 60 lines of C

gist.github.com

21–30 of 110 posts

Re: Generic dynamic array in 60 lines of C

#21

Why use macros instead of inline functions?

Because the API needs to work for random item types. C doesn't have templates (and for stuff like this C11's _Generic isn't helpful because that just maps specialized function names to generic function names, but doesn't help with the generic implementation code).

Re: Generic dynamic array in 60 lines of C

#22
DYN_ARR_RESET should probably be called DYN_ARR_INIT instead, as calling it more than once will leak memory.

The handling of endptr in DYN_ARR_RESIZE seems to be incorrect. If I have an array with 2 elements and capacity of 3 and I DYN_ARR_RESIZE it to 5, I now have an array with 5 elements, 3 of which are garbage values.

Re: Generic dynamic array in 60 lines of C

#24
I like that the comments mention another two implementations of a similar idea and all are a bit different.

Here's one I've written a few years ago: https://github.com/kiryk/mlisp/blob/master/vector.c

You may find it dirty, but it can be wrapped using macros, and used both on LHS and RHS like in "string(v, i) = ch"

(the macros BTW) https://github.com/kiryk/mlisp/blob/71d028738d2f9607fa7ce8c1...

(and a usecase) https://github.com/kiryk/mlisp/blob/71d028738d2f9607fa7ce8c1...

Re: Generic dynamic array in 60 lines of C

#25
post #15

Earlier quoted context omitted.

I don't have this luxury if I want to learn about ffmpeg libraries, its all written in c, almost all media and hardware accelerator libraries are written in c, you go to another language they just bind to c. its soo frustrating but I've no choice but to stick with c.

huh? What's wrong with using the higher level languages if they take care of binding to the C parts for you. (I know these are far an in-between for ffmpeg, too many leaky abstractions.)

Usually increased build system complexity, outdated manually maintained bindings, etc etc... - in some languages (like Zig) this is really trivial, in other languages it can be much harder.

Re: Generic dynamic array in 60 lines of C

#26

Earlier quoted context omitted.

I don't have this luxury if I want to learn about ffmpeg libraries, its all written in c, almost all media and hardware accelerator libraries are written in c, you go to another language they just bind to c. its soo frustrating but I've no choice but to stick with c.

Most languages have the ability to call C functions. Use the language that helps you write your code and convert to call the third party API. For example, a C++ Vector provides a generic container and the code can still call C functions with the underlying array. This is in the "Doctor it hurts if I do X" bucket. https://stackoverflow.com/questions/2923272/how-to-convert-v...

Yes, but if you're writing code that you want to be bindable to other languages, you essentially have to follow the C ABI, in which case, writing in C is a natural choice (yes, you can expose a C ABI from other languages, but that's usually not natural and you sort of have to understand C anyway to do that).

Re: Generic dynamic array in 60 lines of C

#27
post #13

Not an entirely uncommon idea. I've written one. There's also a well-known one here, in klib: https://github.com/attractivechaos/klib/blob/master/kvec.h

Who didn't. Almost any C program dealing with strings and collections has to have their own implementation or import one.

Part of the reason why C developers "feel" productive, but can't produce anything of meaningful complexity.

Re: Generic dynamic array in 60 lines of C

#28

DYN_ARR_RESET should probably be called DYN_ARR_INIT instead, as calling it more than once will leak memory. The handling of endptr in DYN_ARR_RESIZE seems to be incorrect. If I have an array with 2 elements and capacity of 3 and I DYN_ARR_RESIZE it to 5, I now have an array with 5 elements, 3 of which are garbage values.

~~Isn't the handling of `endptr` incorrect in `DYN_ARR_RESET` as well? `a.endptr = a.data;` feels very incorrect to me.~~

Nevermind, I see how it's supposed to work now.

To your point, `RESIZE` is definitely incorrect. It should be checking the length before calling `realloc`.

Re: Generic dynamic array in 60 lines of C

#29

DYN_ARR_RESET should probably be called DYN_ARR_INIT instead, as calling it more than once will leak memory. The handling of endptr in DYN_ARR_RESIZE seems to be incorrect. If I have an array with 2 elements and capacity of 3 and I DYN_ARR_RESIZE it to 5, I now have an array with 5 elements, 3 of which are garbage values.

that's the intended behavior.

this somewhat mirrors what happens with a std::vector when you call resize() except of course c doesn't have default ctors. the point of resize is to make exactly "size" elements of the dynamic array addressable.

Re: Generic dynamic array in 60 lines of C

#30
post #28

DYN_ARR_RESET should probably be called DYN_ARR_INIT instead, as calling it more than once will leak memory. The handling of endptr in DYN_ARR_RESIZE seems to be incorrect. If I have an array with 2 elements and capacity of 3 and I DYN_ARR_RESIZE it to 5, I now have an array with 5 elements, 3 of which are garbage values.

~~Isn't the handling of `endptr` incorrect in `DYN_ARR_RESET` as well? `a.endptr = a.data;` feels very incorrect to me.~~ Nevermind, I see how it's supposed to work now. To your point, `RESIZE` is definitely incorrect. It should be checking the length before calling `realloc`.

Thats just idiomatic C. Set the "pointer to end" to be equal to the "pointer to start".
Post reply on HN