Live data from Hacker News

I write type-safe generic data structures in C

danielchasehooper.com

31–40 of 196 posts

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

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

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

#34
post #31

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

Because for many of the use cases where C is used, switching to C++ involves jumping through even more hoops.

Do you have a couple of real world examples?

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

#35
post #34
post #31

Earlier quoted context omitted.

Because for many of the use cases where C is used, switching to C++ involves jumping through even more hoops.

Do you have a couple of real world examples?

Embedded systems, for example.

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

#36
post #34
post #31

Earlier quoted context omitted.

Because for many of the use cases where C is used, switching to C++ involves jumping through even more hoops.

Do you have a couple of real world examples?

Any established C codebase, for example the kernel or Postgres?

Traditionally microcontroller firmwares as well, though those are increasingly friendly to C++, you just have to be careful about allocations as C++ makes it way easier to accidentally allocate than C does.

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

#37
post #34
post #31

Earlier quoted context omitted.

Because for many of the use cases where C is used, switching to C++ involves jumping through even more hoops.

Do you have a couple of real world examples?

Writing extensions for projects that support C extensions but may not support C++ extensions, e.g. many dynamic languages.

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

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

Ah, that’s what I get for not reading the footnotes. However, the alternative solution presented evaluates the item argument twice, which is problematic as well (but could probably be worked around by passing `(list)->payload` on instead). Secondly, the assignment for type-checking doesn’t work for read-only operations on a const List, or does it? And doesn’t the assignment overwrite the head? Lastly, the do-while construction means you can’t use it for operations that return a value (without compiler extensions).

I also don’t agree it’s “squeamish” to be wary of aliasing analysis going wrong. It’s not a clean abstraction and can hide subtle bugs down the road.

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

#39
post #37
post #34

Earlier quoted context omitted.

Do you have a couple of real world examples?

Writing extensions for projects that support C extensions but may not support C++ extensions, e.g. many dynamic languages.

You can still write the extension in C++ and expose an extern "C" interface.

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

#40
post #34

Earlier quoted context omitted.

Do you have a couple of real world examples?

Embedded systems, for example.

I know it used to be, but is it really still common for embedded systems to use weird architectures that G++/Clang don't support?
Post reply on HN