Does anyone have a favourite data structures library for C? I’m guessing something macro based…?

In particular I’m looking for a linked list (single or double), and a queue. Heap allocated / malloc is fine, though it would be nice if the queue had the option of pre-allocating with a max size.

And along similar lines, anyone have any techniques for making tagged unions easy to work with?

Those are usually so simple it’s worth writing your own for; I rarely use libraries for things that aren’t highly complex (like an rtos or a vendor abstraction layer).

2 Likes

For linked lists I generally prefer to add my own code directly to the class/struct that’s being stored in them. It’s just a matter of adding next/previous pointers to the same class, and static front/back pointers.

For C++ I stick with the good ol’ Standard Template Library.

maybe sys/queue.h and sys/tree.h, from the BSD libc extensions? these are macro-based and most queue operations are O(1).

{ https://github.com/freebsd/freebsd/blob/master/sys/sys/queue.h }
{ https://github.com/freebsd/freebsd/blob/master/sys/sys/tree.h }

2 Likes

That’s certainly one way to abuse the macro system in C. Still interesting though. Makes me wonder what they are used for internally.

Couldnt see a mention of Numerical Recipes In C - this has been a bible for me for years… really handy for basic efficient algorithms, especially useful for embedded stuff…

1 Like

I’ve been successfully navigating some larger C codebases in emacs using xcscope. This emacs/cscope combo is really simple and easy to learn (assuming you’ve already drunk the emacs Koolaid) - highly recommended.

Kind of curious whether cscope is a popular choice in 2020 - I’m sure it’s ancient… Don’t have much experience of the other options (other than grep obviously)

Haven’t used it or heard of it–certainly drunk off the emacs kool-aid so next time I’m browsing a load of C I’ll see if this comes in handy!

I looked into this recently, accidentally bought the examples book (showing use cases of the library code) instead of the book itself.

The code is very strictly licensed so I’m curious if the book itself is a good resource for learning how to implement patterns or just a recipe book that needs to be licensed if you want use it?

Well well well.

GCC 10 defaults to -fno-common :tada:

C language issues

Default to -fno-common

A common mistake in C is omitting extern when declaring a global variable in a header file. If the header is included by several files it results in multiple definitions of the same variable. In previous GCC versions this error is ignored. GCC 10 defaults to -fno-common, which means a linker error will now be reported.

Remember kids, friends don’t let friends C without -fno-common.

3 Likes

ag, or the silver searcher has replaced grep for my code searching needs.

3 Likes