Live data from Hacker News

I write type-safe generic data structures in C

danielchasehooper.com

141–150 of 196 posts

Re: I write type-safe generic data structures in C

#141
post #93

Earlier quoted context omitted.

It is part of the C standard. Whether it is part of a separate binary is an implementation choice.

True on both counts. But they are still separate and distinct languages.

C is a composition of what you describe as separate languages. They are both parts of C. That is why we call unpreprocessed code C code.

Re: I write type-safe generic data structures in C

#142

Earlier quoted context omitted.

When all you have are arrays, everything looks like a problem you solve with arrays. There are quite a few problems that specialised containers are suited for, that's why they were created.

And you can write them when you need them. The situation where you need a red black tree with 10 different key/value combos isn’t real.

If you can only write programs by banging rocks together, you will only produce programs that can be written by banging rocks together.

Re: I write type-safe generic data structures in C

#143

Another way is to not try to write generic data structures. When you tailor them to the use case you can simplify. The #1 data structure in any program is array.

The irony is that arrays in C are in fact generic. As that is the simplest generic solution that doesn't require boiling the ocean first, that's what many programmers reach for.

Re: I write type-safe generic data structures in C

#144
post #133

Earlier quoted context omitted.

Surely part of the problem is having a distinct term and handling for parameters passed to functions. What is the point? It seems confusing with no upside.

Do you find the difference between abstract and concrete confusing? Or the difference between container and contents? Is that a pointless distinction with no upside?

I do agree these are useful concepts to distinguish, but I don't get the connection to the topic at-hand. To me, there is just the function signature. I don't see a benefit to referring to passed values as distinct from received values. To my ear "argument" and "parameter" are perfect synonyms.

Re: I write type-safe generic data structures in C

#145
Interesting but too complicated for me.

For my own hashmap implementation I followed a wasteful aproach since I’m. It targeting embedded.

I created a structure called a hashmap object. It has two elements: a void pointer and a char pointer. The first one is the data and the second one is the metadata. The metadata is basically a string were the user can put anything, the type of the data, more data, whatever.

Then I preallocate 10s of thousands of hashmap objects. That way users of my hashmap don’t have to think about aollocating and de allocating hashmap nodes, they just insert, delete and search freely. They still have to care about allocating and de allocating they’re own data though.

Re: I write type-safe generic data structures in C

#146
post #140
post #46

Earlier quoted context omitted.

That's possible, but then the people building your extension need a C++ toolchain. The question was "please provide examples where switching to C++ involves jumping through even more hoops", and in my view requiring downstream to use a C++ environment when they're expecting to use a C environment qualifies.

Most C compilers are C++ toolchain as well, we are no longer in the 1990's. Unless of course the project is using such a old compiler.

Problems in this domain arise more because you’re wandering off the beaten path for configuration and tooling than because the systems lack absolute capabilities. If you don’t want to build your extension the way that the extension framework expects you, all sorts of annoyances show up. Maintaining an cross-platform compatible C extension is hard enough already.

Re: I write type-safe generic data structures in C

#147

> Structurally identical types will be considered the same type in GCC 15 and Clang later in 2025 thanks to a rule change Beware that only tagged unions are considered the same type under the new rule, provided they have the same structrure and the same tag. The List(T) macro should be changed to generate a different tag for each different T. Which is trivial (with ##) for simple one-word types, but impossible for ev…

> Beware that only tagged unions are considered the same type I don't get it. Tagged union is just a design pattern.

By tagged they mean have an identifier. Compare

  > struct { ... } foo;
and

  > struct bar { ... } foo;
The latter has an identifier, bar; the former doesn't. The standard uses tag to refer to the identifier name, if any, in an enum, struct, or union declaration.

Re: I write type-safe generic data structures in C

#149
post #60

Earlier quoted context omitted.

i am firmly of the opinion that compiling to c is a better route than doing clever c tricks to sort of get what you want. the compiler can be pretty minimal and as you note it pays for itself.

There’s some prior work called CFront. It implements a superset of C that’s just an “increment”. I think it’s worth looking into, it might take off one day!

I dunno, I looked at that a while back - the version for MS-DOS shipped on multiple floppy disks. Not exactly lightweight!

Re: I write type-safe generic data structures in C

#150

I think the idea of using a union to store the element type without any extra run-time memory cost might have some use, specifically in cases where the container struct wouldn't typically store a variable of the element type (or, more likely, a pointer to the element's type) but we want to slip that type information into the struct anyway. However, the problem that I have with this idea as a general solution for gene…

[deleted]
Post reply on HN