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.)
Generic dynamic array in 60 lines of C
31–40 of 110 posts
Re: Generic dynamic array in 60 lines of C
#32Re: Generic dynamic array in 60 lines of C
#33Heavy use of macros to do metaprogramming is a strong sign it's time to move to a more powerful language.
Re: Generic dynamic array in 60 lines of C
#34yes, i know growth by a factor of 2 has issues under certain usage patterns. those are less likely to be problematic when there is more stuff going on than just growing the buffer - you can use the "hole" for other allocations.
[0] https://rkeene.org/viewer/tmp/hat.c.htm [1] https://rkeene.org/viewer/tmp/hat.png.htm
Re: Generic dynamic array in 60 lines of C
#35Heavy use of macros to do metaprogramming is a strong sign it's time to move to a more powerful language.
C + a set of macros is a more powerful programming language.
If heavy macro usage is found, it's definitely time to reconsider the approach.
Re: Generic dynamic array in 60 lines of C
#36Re: Generic dynamic array in 60 lines of C
#37There are long term ABI benefits to these data structures just being opaque pointers outside of the implementing libraries
Re: Generic dynamic array in 60 lines of C
#38Not 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
#39As fpoling points out, capacity is a 32-bit unsigned. That can overflow. There is no safety in append: if capacity is zero no new space will be made, if capacity overflows the realloc will not have enough space -- in either case, you end up writing past the end.
Allocating a [capacity] zero array and appending to it is extremely common. That you'll write past the end in that case shows that the author has barely even used this.
The code is unacceptably bad and unsafe. I don't usually do this, but I'm going to flag this post. I encourage everybody to do the same.
Apologies to the author. Golf is fun. This is uncool.
edit: changed size to [capacity]
Re: Generic dynamic array in 60 lines of C
#40Earlier quoted context omitted.
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).