Live data from Hacker News

Generic dynamic array in 60 lines of C

gist.github.com

1–10 of 110 posts

Re: Generic dynamic array in 60 lines of C

#4

data (startptr), endptr & capacity?! Memory might be cheap enough to waste, but more derefs do not help timings.

Three pointers (or two and a size, or one and two sizes) is pretty standard for dynamic arrays. It lets you allocate more than one element a time when repeatedly appending one element.

No checking malloc or realloc though. The anonymous struct is dubious too, though maybe being unable to pass these things to functions is a feature.

Re: Generic dynamic array in 60 lines of C

#7

data (startptr), endptr & capacity?! Memory might be cheap enough to waste, but more derefs do not help timings.

Three pointers (or two and a size, or one and two sizes) is pretty standard for dynamic arrays. It lets you allocate more than one element a time when repeatedly appending one element. No checking malloc or realloc though. The anonymous struct is dubious too, though maybe being unable to pass these things to functions is a feature.

functions can work with just pointer / size / stride. think of it as stl iterators, there's no real point to be passing the container itself around.

however, you can still do that, if you really want.

just `typedef DYN_ARR_OF(widget) widget_array;` and now you have a name-able type, and can even have dynamic-arrays-of-dynamic-arrays (`DYN_ARR_OF(widget_array)`).

Re: Generic dynamic array in 60 lines of C

#9

Heavy use of macros to do metaprogramming is a strong sign it's time to move to a more powerful language.

Yeah that's a great opinion and all but generally C is used because it HAS to be used these days. No one is going to use this code for building a web application or will be tossing it into legacy code.

Re: Generic dynamic array in 60 lines of C

#10
This seems pretty sensical to me except that it achieves its small size by sacrificing error checking and handling, even in the (aiui) non-exceptional case of realloc() returning NULL. This is of course classic C program behaviour so perhaps that's fine ;-)
Post reply on HN