News for hackers. Stuff that matters.
Re:Collections-C, generic data structures for C by akp__ on Sunday March 13, 2016 @02:48PM (#11278820)
I'm no expert here, just curious. I've seen this brought up a couple times now: what's wrong with void pointers? How else would you do this in C? If you wanted abstractions to help with pointer lifetimes wouldn't you just be better off using C++?
Re:Collections-C, generic data structures for C by lucozade on Sunday March 13, 2016 @03:30PM (#11278975)
The limitation with void pointers is that it can only inline values <= the size of a pointer. If you have something larger then you need to allocate it separately which may have cache efficiency issues.

The main alternative, other than hardcoding an element type, is to use macros and pass the element type in the definition.

Sometimes you just don't have the option of a C++ compiler. Also, and this was a situation I was in recently, I needed a collection in a small area of my C code so it really didn't warrant a toolset change.

Re:Collections-C, generic data structures for C by woodman on Monday March 14, 2016 @02:33AM (#11281379)
Void pointers mean no type safety, preventing a lot of potential compiler optimizations and static analysis checks. 99% of the time the right thing to do is just create a new type:

  typedef struct Strref_t {
    char* str;
    size_t ref;
  } Strref;
There, just pass a pointer of that type around.