10

In C++ you can have both generic and type safe containers by using templates. However in C, if you want generic containers, you have to (afaik) use void*, which means you lose type safety. To have type safe containers, you would have to reimplement them for every type of data you want to hold.

Given that C follows a more the-programmer-knows-what-he's-doing philosophy than C++, what would be the more idiomatic thing to do in C: use generic containers with void*, or make custom containers for every type of data?

Paul Manta
  • 30,618
  • 31
  • 128
  • 208

4 Answers4

4

I'd aim for generic containers:

  1. Once you get used to it, you just think of void * is meaning the type of something when I don't care about it's type. It's like Object in Java -- where, for a long time, generic containers didn't have type safety either.

  2. You only have one place to make improvements.

  3. You don't get the type safety; but with repeated implementations of type safe containers, you run the risk of copy and paste errors. That can lead to errors, too.

Edmund
  • 10,533
  • 3
  • 39
  • 57
1

So I had a quick search to see if any new clever ideas had emerged in the field of C containers.

I found this. It's a pretty thorough attempt at a complete C container library.

It passes around the elements to be contained in void * pointers. There is no attempt to specify structures of a particular type.

For type safety you COULD write a "wrapper" macro for each container, that defines a host if inline functions that wrap around an untyped version and provide the type safety. Hopefully they would be optimized away by the compiler, but this isn't always possible. And the macros would be ugly.

I guess this is one of the reasons why more (application) programs are written in C++ instead of C. The ability to create complex abstractions in C is limited. You can do it but you generally sacrifice other aspects like performance or maintainability (check out a C GTK program for example).

Michael Slade
  • 13,802
  • 2
  • 39
  • 44
  • With the new `_Generic` keyword of C11, those macros musn't be too ugly, actually. Clang already implements this, and gcc also has enough features to emulate it. – Jens Gustedt Apr 29 '12 at 08:28
0

To make a type which handles generic data in C you would use a void* to pass the data around. It is impractical to define new algorithms for each possible type.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

you've no real benefit to using void* as a member (unless, say, compilation times are most important), you can always cast to void* where appropriate, then operate on untyped data blobs where necessary.

i've implemented such interfaces in C (e.g. multiple variants with named fields of different types). i favored having the type safety, and rarely found casting to void* a good thing in these implementations.

…then again, i spend more time writing c++ than c :)

justin
  • 104,054
  • 14
  • 179
  • 226