Live data from Hacker News

I write type-safe generic data structures in C

danielchasehooper.com

81–90 of 196 posts

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

#81

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.

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.

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

#83
post #33
post #22

The casting of the function type assumes that the item pointer type (e.g. Foo*) has the same representation as void*, which the C standard doesn’t guarantee (in standardese: the two types aren’t “compatible”). Calling the function with the converted type therefore constitutes undefined behavior. It also impacts aliasing analysis by compilers (see [0], incidentally), even if the pointer representation happens to be th…

This is addressed in the footnotes. casting is not the core of the type safety. Read the whole article.

Sure, but your alternative code incorrectly assigns to (list)->payload. You have many other options. Without typeof, you can if(0) the assignment, or check type compatibility with a ternary operator like 1 ? (item) : (list)->payload and pass that to _list_prepend, etc. With typeof, you can store item in a temporary variable with the same type as (list)->payload, or build a compound literal (typeof(*(list))){.payload=(item)}, etc.

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

#84

The “typeof on old compilers” section contains the code: (list)->payload = (item); /* just for type checking */\ That is not a no-op. That is overwriting the list head with your (item). Did you mean to wrap it in an `if(0)`?

In that example they also had replace the union with a struct - presumably to work around this issue. But that seems wasteful to me too. Doing it within an if(0) seems strictly better.

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

#86

Why would you jump through all these hoops instead of just writing C++ if you want "C with generics"

Why would you write C++ if you can get the same result by jumping through a few hoops with C?

Templates in C++ require language support - you can't simply implement them with "a few hoops" in C.

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

#87
post #72

Earlier quoted context omitted.

If I may may be provocative :-) this post isn't about C. It's about layering on a custom language using C preprocessor macros. My compilers were originally written in C. I started using the C preprocessor to do metaprogramming. After some years I got fed up with it and removed nearly all of the preprocessor use, and never looked back. My code was much easier to understand. An amusing story: long ago, a friend of mine…

If the C compiler accepts it, it is C.

There is no one "the C compiler".

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

#88
post #72

Earlier quoted context omitted.

If I may may be provocative :-) this post isn't about C. It's about layering on a custom language using C preprocessor macros. My compilers were originally written in C. I started using the C preprocessor to do metaprogramming. After some years I got fed up with it and removed nearly all of the preprocessor use, and never looked back. My code was much easier to understand. An amusing story: long ago, a friend of mine…

If the C compiler accepts it, it is C.

Pedantically, the preprocessor is an entirely separate language. The lexing, parsing, expressions, and semantics are totally distinct. The preprocessor is usually implemented as a completely independent program. My first C compiler did integrate the preprocessor with the C compiler, but that was for performance reasons.

Currently, ImportC runs cpp and then lexes/parses the resulting C code for use in D.

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

#89
post #33

Earlier quoted context omitted.

This is addressed in the footnotes. casting is not the core of the type safety. Read the whole article.

Sure, but your alternative code incorrectly assigns to (list)->payload. You have many other options. Without typeof, you can if(0) the assignment, or check type compatibility with a ternary operator like 1 ? (item) : (list)->payload and pass that to _list_prepend, etc. With typeof, you can store item in a temporary variable with the same type as (list)->payload, or build a compound literal (typeof(*(list))){.payload=…

The assignment is intentional. The union changed to a struct

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

#90
post #27

I'm curious what a hashmap looks like with this approach. It's one thing to pass through or hold onto a generic value, but another to perform operations on it. Think computing the hash value or comparing equality of generic keys in a generic hashmap.

I first would question what a user wants to do with a hashmap that uses polymorphic key-values of unknowable type at compile-time.

As a thought experiment, you could certainly have users define their own hash and equality functions and attach them to the table-entries themselves. On first thought, that sounds like it would be rife with memory safety issues.

At the end of the day, it is all just bytes. You could simply say that you will only key based on raw memory sequences.

Post reply on HN