Live data from Hacker News

Generic dynamic array in 60 lines of C

gist.github.com

71–80 of 110 posts

Re: Generic dynamic array in 60 lines of C

#71

I don't like the use of macros for things like this. Macros in general should rarely or sparingly be used. I'm also not certain one should use "end pointers." Conventionally, it seems more advisable to use `size_t capacity`, `size_t length`, and `void *data`. Great use of Cunningham's Law, though! I appreciate C posts on Hacker News.

the alternative in c is something like glib's array container which hides types. I dislike that more.

Re: Generic dynamic array in 60 lines of C

#72

Earlier quoted context omitted.

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 use…

> when they're used properly they're not Yeah, they are. Source: Decades of C programming

You're not the only person with decades of experience.

Re: Generic dynamic array in 60 lines of C

#73

I don't like the use of macros for things like this. Macros in general should rarely or sparingly be used. I'm also not certain one should use "end pointers." Conventionally, it seems more advisable to use `size_t capacity`, `size_t length`, and `void *data`. Great use of Cunningham's Law, though! I appreciate C posts on Hacker News.

the alternative in c is something like glib's array container which hides types. I dislike that more.

Yes, but GLib arrays are also lossy. They require you to externally manage capacity or to know with perfect foresight exactly what your capacity will be for the lifetime of the memory allocation.

I don't think those are impossible scenarios, but the cost of one additional pointer in terms of size leaves you with a lot more functionality. Saving one pointer in size and not having capacity makes GLib arrays nearly useless, which I find confusing.

You could simply pass a pointer and a size around instead. Which is what most people actually do when they don't need resizable data layouts.

If you're working with C, I think you have to just accept that void pointers happen. Working around losing compile-time type data requires you to create runtime structures, which I don't find acceptable.

Re: Generic dynamic array in 60 lines of C

#74
post #34

yes, 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.

Hard to tease out what you're saying here, but maybe a Hashed Array Table (HAT) is what you're referring to ? I have a slightly extended version here [0][1] [0] https://rkeene.org/viewer/tmp/hat.c.htm [1] https://rkeene.org/viewer/tmp/hat.png.htm

I think GP meant that his implementation doubles the capacity of the array on resize as opposed to increasing by 1.5 or some other factor.

Re: Generic dynamic array in 60 lines of C

#75

I don't like the use of macros for things like this. Macros in general should rarely or sparingly be used. I'm also not certain one should use "end pointers." Conventionally, it seems more advisable to use `size_t capacity`, `size_t length`, and `void *data`. Great use of Cunningham's Law, though! I appreciate C posts on Hacker News.

Amusingly, Ward Cunningham denies inventing Cunningham's law and I feel compelled to correct that potential misunderstanding.

Re: Generic dynamic array in 60 lines of C

#76

Earlier quoted context omitted.

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.

til the Linux kernel has no meaningful complexity

Still waiting for a revolutionary OS written in $virtuous_lang to save us from the scourge of unsafe Linux. They've had since Ada-83 to get something off the ground. Guess they aren't so productive either.

Re: Generic dynamic array in 60 lines of C

#77

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.

The values are garbage because they haven't been initialized yet. Other code may write to those locations.

Re: Generic dynamic array in 60 lines of C

#78

I don't like the use of macros for things like this. Macros in general should rarely or sparingly be used. I'm also not certain one should use "end pointers." Conventionally, it seems more advisable to use `size_t capacity`, `size_t length`, and `void *data`. Great use of Cunningham's Law, though! I appreciate C posts on Hacker News.

One should always use size_t to keep lengths. Reallocating the array invalidates all pointers but does not invalidate offsets.

Re: Generic dynamic array in 60 lines of C

#79

Earlier quoted context omitted.

> when they're used properly they're not Yeah, they are. Source: Decades of C programming

You're not the only person with decades of experience.

We all suffer from this, so it's not personal, but one is too often blind to the shortcomings of one's own code.

Re: Generic dynamic array in 60 lines of C

#80
awesome! i do the same thing for arrays[1] and maps[2].

stuff like this is great when you are trying to find the performance ceiling of some workload in c/cpp. literally nothing to hide.

1. https://github.com/nathants/bsv/blob/master/util/array.h

2. https://github.com/nathants/bsv/blob/master/util/map.h

Post reply on HN