News for hackers. Stuff that matters.
Re:Collections-C, generic data structures for C by jepler on Sunday March 13, 2016 @10:06AM (#11277722)
It looks like all collections store void pointers. I don't see any abstractions to help with pointer lifetimes, which remains the main problem I encounter when trying to write large programs in C that dynamically allocate memory.
Re:Collections-C, generic data structures for C by yoklov on Sunday March 13, 2016 @11:16AM (#11277953)
Start using arenas wherever you can. Makes memory management in C programs substantially simpler.
Re:Collections-C, generic data structures for C by eatonphil on Sunday March 13, 2016 @01:02PM (#11278357)
Any good readings on getting started with arenas?
Re:Collections-C, generic data structures for C by pieterr on Sunday March 13, 2016 @01:29PM (#11278484)
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.
Re:Collections-C, generic data structures for C by stinos on Sunday March 13, 2016 @12:35PM (#11278240)
It looks like all collections store void pointers

Does that mean that if you're running as 32bit and want to store a number larger than that (a double or int64_t or so) or a POD that all of these have to be allocated seperately?