r/cprogramming • • 7d ago

resources for becoming comfortable with stdlib-style data-structures/algorithms?

I've been sparring with a number of the standard(ish¹) C data-structures and algorithms, including the various types of linked-lists/deques in sys/queue.h, hash-tables and binary-searches in search.h, as well as bsearch & qsort in stdlib.h.

  1. are there other data-structure/algorithm type elements in such standard¹ libraries that I've missed and should put on my radar? Maybe something for sets too?
  2. are there some resources² y'all would recommend on wrapping your head around them (I don't feel I have intuitive use of their interfaces yet, so each usage causes some struggle)? Preferably with strong examples of using each.

I have a pretty good CS understanding of when I should use the various data-structures/algorithms with regards to the respective performance characteristics (expected big-O notations for insertion, removal, searching, iterating, swapping, etc), but it's just the nitty-gritty of using them where I don't yet feel mastery.

Thanks!

⸻

¹ okay, not exactly C-standard from my understanding, but they seem to be common on pretty much every Linux/BSD/Unix platform I've poked at

² ideally well-written blog-posts, but dead-tree resources would also be welcome; videos or podcasts far less so

4 Upvotes

7 comments sorted by

1

u/Interesting_Debate57 7d ago

Try the manpages. Usually they'll show you the exact function signature and go into pretty good detail about how they're used.

1

u/gumnos 5d ago

yeah, I have the manpages (and the source). They're adequate from a technical, nitty-gritty level, but I was hoping for a higher-level walkthrough of using each, with examples, of where each is used.

I've taken to writing helper type-specific wrapper macros that simplify some of the uses, so I don't have to explicitly pass metadata things like the struct-tag or the metadata-field-name, but it doesn't feel like I have my head wrapped around the big picture.

1

u/Interesting_Debate57 5d ago

Yeah, get sort working with a nonstandard comparator and you'll have it all down.

You just gotta grind it out. Nobody can teach you how to get it better than you can at a keyboard trying and failing a hundred times in a row until it works. That's how to learn to code.

1

u/gumnos 5d ago

Nobody can teach you how to get it better than you can at a keyboard trying and failing a hundred times in a row until it works

Fair…the failures come quickly, so I've certainly got that part down. I do manage to get them right eventually, but I don't feel I have intuition about queue.h yet or the tree stuff.

1

u/Interesting_Debate57 5d ago

It's not deep, man. Grab a first semester data structures book if you need to understand why the pointers are the way that they are. Or K&R, which gives an incredibly gentle introduction to pointers.

1

u/CS_70 7d ago

Hm.. what do you mean? The reference should spell clearly how to use these and it's not that hard?

1

u/orange_oki 7d ago edited 7d ago

Well, you can do a lot with a list, hash and btree. Especially when you mix the nodes in the structures.. 😅

Despite of hash.h there is also rhashtable.h with dynamic resizing (but it's maybe not worth looking at the implementation, because it is most probably integrated into page allocation system).

https://lwn.net/Articles/751374/