Live data from Hacker News

Collections-C, generic data structures for C

github.com

21–30 of 63 posts

Re: Collections-C, generic data structures for C

#21
post #19

Earlier quoted context omitted.

But for this kind of application, wouldn't you just panic and do a soft restart on malloc failure anyway? Actually recovering from an out-of-memory situation is incredibly fraught and unreliable; most of the realtime operating systems I've used don't even bother to try.

Even then you'd want proper logging and panicking over a segfault (the likely result).

In such an environment, malloc() will do that.

Re: Collections-C, generic data structures for C

#23
post #2

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.

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

#24
Nice, but it's rather naive and through that - needlessly wasteful.

It's C.

Why on Earth you'd want to allocate a separate list node for each piece of data when you can embed this node directly into the data and then use container_of or similar offsetof() derivative to get a pointer to the data by a pointer to a list item? Saves you at least sizeof(void*) per item and eliminates a chance of list_add ever failing among other things.

The same goes for custom allocators - why would you drag around pointers to malloc, calloc and free, when all you need is just a pointer to realloc?

This sort of thing. The code is nice, but it's not how one would write a container library after few years of hands-on C experience.

Re: Collections-C, generic data structures for C

#25
post #6
post #4

Has this library been stress tested? I am interested in using this library for a spaceflight application, but reliability is paramount in our situation.

I stopped looking when I saw it doesn't check for malloc failure. I am going to say space flight is a no-go.

Ouch. I'll keep looking then.

Any recommendations for reliable C data structures?

Re: Collections-C, generic data structures for C

#26
post #6
post #4

Has this library been stress tested? I am interested in using this library for a spaceflight application, but reliability is paramount in our situation.

I stopped looking when I saw it doesn't check for malloc failure. I am going to say space flight is a no-go.

Thank you. There is somebody else on this fucking planet who understands that memory is not limitless manna from heaven.

Re: Collections-C, generic data structures for C

#27
Honestly, why? Most of the time, you want to write C++, with exceptions, and the STL. You can make this kind of programming as robust as you want against memory allocation failure.

If you don't want to use this style of programming, for whatever reason, check out sys/queue.h. It's already on your system, if you're using some kind of Unix.

Re: Collections-C, generic data structures for C

#28
post #12
post #4

Has this library been stress tested? I am interested in using this library for a spaceflight application, but reliability is paramount in our situation.

Give Ada a try

[Un]fortunately, we are tied to C/C++ due to mission requirements and our hardware platform.

Re: Collections-C, generic data structures for C

#29
post #23
post #2

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.

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++?

The limitation with void pointers is that it can only inline values 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

#30
post #6

Earlier quoted context omitted.

I stopped looking when I saw it doesn't check for malloc failure. I am going to say space flight is a no-go.

But for this kind of application, wouldn't you just panic and do a soft restart on malloc failure anyway? Actually recovering from an out-of-memory situation is incredibly fraught and unreliable; most of the realtime operating systems I've used don't even bother to try.

That depends on when a failure were to occur. If we have a failure while the spacecraft is en route to its destination, perhaps we can recover because we may have enough time.

If we have a failure during rendezvous with our target, it could be a very bad day.

Post reply on HN