Live data from Hacker News

Generic dynamic array in 60 lines of C

gist.github.com

51–60 of 110 posts

Re: Generic dynamic array in 60 lines of C

#51
post #23

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

Lisp?

When it comes to macros, accept no substitute.

Granted, Lisp already has arrays, so not much reason to reinvent the wheel.

  (make-array '(2 6) :initial-element 0 :element-type '(unsigned-byte 32))
  ;; => #2A((0 0 0 0 0 0) (0 0 0 0 0 0))

Re: Generic dynamic array in 60 lines of C

#53
post #40
post #26

Earlier quoted context omitted.

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).

Unless your API surface constitutes the majority of your code, writing in C still doesn't make sense, because you're paying the tax every time you have to manually juggle strings and whatnot. C++ is a more sensible choice for this - you can still write C-compatible headers (and it's trivial to verify in builds - just include it in a .c file and see if that compiles!) while using STL etc in the implementation.

Yeah if you're using opaque handles (where what you have is really a pointer to some C++ thing), this can work well.

If you're exposing structs that are passed back and forth for state, it's a bit more cumbersome.

Re: Generic dynamic array in 60 lines of C

#54
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…

Is this overflow really a realistic scenario? I imagine most machines won't even permit you to allocate the sorts of sizes that would cause this bug.

In that scenario (where you're allocating a multi-gigabytes array), you typically know the size ahead of time, instead of growing it dynamically, as the latter doesn't really perform too well.

Re: Generic dynamic array in 60 lines of C

#55

I think this author should learn about the "do {} while(0)" trick.

What's the benefit of do {...} while (0) vs just {...} ?

The latter makes the macro usable without requiring a semicolon, which some people don't like; the former will cause a syntax error if the semicolon is missing, so they feel that it regulates syntax a bit better.

Re: Generic dynamic array in 60 lines of C

#56
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…

[deleted]

Re: Generic dynamic array in 60 lines of C

#57

I think this author should learn about the "do {} while(0)" trick.

What's the benefit of do {...} while (0) vs just {...} ?

Works if you want to e.g. skip braces if if..else... statements. Some would say putting braces in always is good style, but I like skipping them myself so won't defend it :)

Re: Generic dynamic array in 60 lines of C

#58

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

On one hand that's obviously true, on the other hand it's just 60 lines of code, which is easier to check and audit than (for instance) a typical std::vector implementation - I don't know what the equivalent is in D, please forgive my ignorance :)

A typical std::vector implementation supports a lot more things, so it's not an apples-to-apples comparison. It's like saying that a horse-drawn carriage is a lot easier to repair than a motor vehicle. It's true, but doesn't really say much.

Re: Generic dynamic array in 60 lines of C

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

til the Linux kernel has no meaningful complexity

Re: Generic dynamic array in 60 lines of C

#60

I think this author should learn about the "do {} while(0)" trick.

What's the benefit of do {...} while (0) vs just {...} ?

do {…} while (0) is a statement, so it can be put between “if (…)” and “; else”, while just {…} would result in a syntax error due to a hanging else.
Post reply on HN