3 ms·
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
by jepler 11y ago
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.
- yoklov 11y agoStart using arenas wherever you can. Makes memory management in C programs substantially simpler.
- stinos 11y agoIt 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?
- akp__ 11y agoI'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++?
- lucozade 11y agoThe 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.
- woodman 11y agoVoid 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.