Live data from Hacker News

Generic dynamic array in 60 lines of C

gist.github.com

41–50 of 110 posts

Re: Generic dynamic array in 60 lines of C

#43
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.

You hurt me with the truth.

Anyway, I think this HN posting has this "crazy" flavor of using macros to simulate generics, and that's the specific kind of implementation that I meant, which klib also does.

Re: Generic dynamic array in 60 lines of C

#44
post #39

DON'T USE THIS As 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…

I have been using this for years. Capacity is not size. A zero size array will have non zero capacity. All c code is unsafe.

Re: Generic dynamic array in 60 lines of C

#45
post #37

I dislike this - having the “array” be a struct containing the pointer and size fields makes it easy to “copy” the array such that you get dangling pointers. Similarly there’s no way to track lifetime or ownership of the array. There are long term ABI benefits to these data structures just being opaque pointers outside of the implementing libraries

no reason for it to ever cross abi boundary.

Re: Generic dynamic array in 60 lines of C

#46
post #39

DON'T USE THIS As 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…

I have been using this for years. Capacity is not size. A zero size array will have non zero capacity. All c code is unsafe.

Where do you check that capacity is nonzero? When capacity is zero, what happens on this line?

     a.capacity 
"all c code is unsafe" is not an excuse to permit bloody obvious, undocumented memory overruns.

I write a lot of c. Avoiding the unsafe bits, avoiding UB, is the skill required to write good c. "C code is unsafe" is a Rustacean marketing slogan. Don't believe it, but definitely don't practice it.

Re: Generic dynamic array in 60 lines of C

#47
What does it actually gain by using macros instead of proper functions? The only generic macro that can't be written in function is the one use `type`, but saving the type size in the struct is enough for this kind of code.

Re: Generic dynamic array in 60 lines of C

#48

Earlier quoted context omitted.

C + a set of macros is a more powerful programming language.

Also a far worse language though. Macros have their place, but trying to do anything complex with them just turns into a total nightmare. They're hard to reason about, walk through, or modify. If heavy macro usage is found, it's definitely time to reconsider the approach.

It really depends on how macros are used. If you're just using them to implement "high level language features", it's not a problem; sure, you might have trouble figuring out what STAILQ_INSERT_TAIL does internally, but you're going to have just as much trouble figuring out what the Lisp or Perl or Python "add this item to the end of that list" operations do internally.

Macros can be a nightmare, but when they're used properly they're not.

Re: Generic dynamic array in 60 lines of C

#49
post #46

Earlier quoted context omitted.

I have been using this for years. Capacity is not size. A zero size array will have non zero capacity. All c code is unsafe.

Where do you check that capacity is nonzero? When capacity is zero, what happens on this line? a.capacity "all c code is unsafe" is not an excuse to permit bloody obvious, undocumented memory overruns. I write a lot of c. Avoiding the unsafe bits, avoiding UB, is the skill required to write good c. "C code is unsafe" is a Rustacean marketing slogan. Don't believe it, but definitely don't practice it.

just don't initialize it with 0 capacity :-)

Re: Generic dynamic array in 60 lines of C

#50
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.

[deleted]
Post reply on HN