TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Common libraries and data structures for C

322 pointsby tirrexabout 3 years ago

22 comments

unwindabout 3 years ago
Okay, I love C so of course I had to take a look. The buzzwords are impressive, with all the testing and CI and so on, really nice, modern and ambitious.<p>I dove into the code, literally looking at the first true part of the implementation in array&#x2F;sc_array.h.<p>Two observations, from probably less than tree minutes of reading:<p>1. The nomenclature with &quot;sc_array_term()&quot; as the destructor (the opposite of sc_array_init()) was new to me; I&#x27;m not saying that&#x27;s a real problem but it&#x27;s at least adding friction in a way. I would have expected sc_array_free(), but that seems to be internally used as an alias for the standard function free(). Very confusing, since that name (sc_array_free()) then leaks into the source but it has the feeling of being meant for internal use.<p>2. I wonder if this has a bug:<p><pre><code> &#x2F;** * Deletes items from the array without deallocating underlying memory * @param a array *&#x2F; #define sc_array_clear(a) \ do { \ (a)-&gt;cap = 0; \ (a)-&gt;size = 0; \ (a)-&gt;oom = false; \ } while (0) </code></pre> In my experience, when you want to clear a dynamic array but keep the allocated memory, the &#x27;capacity&#x27; (&#x27;cap&#x27;, here) field should <i>not</i> be cleared. I think this will leak memory if an array is grown, cleared, and then re-filled since the sc_array_add() function will see a zero capacity, and allocate new storage.<p>Just my inflation-devalued SEK 0.02, and I did <i>not</i> run the code, I just read it very quickly. Corrections welcome.
评论 #31407401 未加载
评论 #31411073 未加载
评论 #31407289 未加载
评论 #31434947 未加载
评论 #31414537 未加载
评论 #31409677 未加载
begriffsabout 3 years ago
As I got more into C programming, I started looking for data structure libraries. Found a few [0]. Also evaluated sc, but it had too much pre-processor magic for my taste. It also bundles random &quot;stuff&quot; like a URI parser, a thread abstraction, etc.<p>Eventually I rolled my own [1] more focused library. It&#x27;s basic and portable.<p>0: <a href="https:&#x2F;&#x2F;begriffs.com&#x2F;posts&#x2F;2020-08-31-portable-stable-software.html#compensating-for-the-standard-library" rel="nofollow">https:&#x2F;&#x2F;begriffs.com&#x2F;posts&#x2F;2020-08-31-portable-stable-softwa...</a> 1: <a href="https:&#x2F;&#x2F;github.com&#x2F;begriffs&#x2F;libderp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;begriffs&#x2F;libderp</a>
评论 #31406250 未加载
评论 #31406516 未加载
评论 #31406276 未加载
WickedSmokeabout 3 years ago
Good stuff. I may make use of the .ini parser as my current one doesn&#x27;t handle sections.<p>There are a growing number of stand alone support modules in my projects that I need to publish as a collection some day. Here&#x27;s a couple links to some of them:<p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;WickedSmoke&#x2F;faun&#x2F;tree&#x2F;master&#x2F;support" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;WickedSmoke&#x2F;faun&#x2F;tree&#x2F;master&#x2F;support</a><p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;xu4-engine&#x2F;u4&#x2F;tree&#x2F;master&#x2F;src&#x2F;support" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;xu4-engine&#x2F;u4&#x2F;tree&#x2F;master&#x2F;src&#x2F;support</a><p>These include more exotic stuff like a grid based field-of-view calculation and a version of the sfxr synthesizer.
israrkhanabout 3 years ago
For people advocating use of C++ instead of C, keep in mind there are several platforms (mostly embedded) that only support C and not C++. Also there are many projects that make use of C only. If C++ is available, I agree one should use it, however that is not always the choice.
评论 #31406166 未加载
评论 #31406522 未加载
评论 #31406083 未加载
评论 #31406539 未加载
评论 #31406183 未加载
alcoverabout 3 years ago
There&#x27;s the Clib initiative at <a href="https:&#x2F;&#x2F;github.com&#x2F;clibs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;clibs</a>. I don&#x27;t know how much they curate&#x2F;review their entries.<p>As often said, <i>apt install foo</i> is also a bit of a package manager for C.<p>Maybe we should establish a sort of expert-led central archive of rock-solid, battle-tested C libs&#x2F;functions&#x2F;snippets that one can trust ?
评论 #31407913 未加载
xvilkaabout 3 years ago
Interesting project. We have a similar set of various cross-platform helpers for various C structures and tasks during all these years within Rizin - RzUtil[1][2]. They are more tightly coupled with each other though and is LGPLv3-licensed.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;rizinorg&#x2F;rizin&#x2F;tree&#x2F;dev&#x2F;librz&#x2F;util" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rizinorg&#x2F;rizin&#x2F;tree&#x2F;dev&#x2F;librz&#x2F;util</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;rizinorg&#x2F;rizin&#x2F;tree&#x2F;dev&#x2F;librz&#x2F;include&#x2F;rz_util" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rizinorg&#x2F;rizin&#x2F;tree&#x2F;dev&#x2F;librz&#x2F;include&#x2F;rz_...</a>
darknaviabout 3 years ago
I work in C++ daily and there is something about the simple-ness of C that I love. You get out of magical hell that is templates and return to simple flat-functions and macros.
评论 #31406085 未加载
评论 #31406280 未加载
评论 #31405990 未加载
评论 #31406544 未加载
评论 #31407221 未加载
评论 #31406141 未加载
glouwbugabout 3 years ago
Good stuff. void* in itself is great for generics. C does support a templating system, like C++, with a little (reasonable) preprocessor abuse. It gives great cache contiguous results: <a href="https:&#x2F;&#x2F;www.github.com&#x2F;glouw&#x2F;ctl" rel="nofollow">https:&#x2F;&#x2F;www.github.com&#x2F;glouw&#x2F;ctl</a>
评论 #31404893 未加载
评论 #31406469 未加载
评论 #31405187 未加载
评论 #31406173 未加载
RcouF1uZ4gsCabout 3 years ago
With apologies to Greenspun:<p>Any sufficiently complicated C program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of C++.
评论 #31410655 未加载
somerando7about 3 years ago
The lengths people will go to not use C++ is staggering.
评论 #31406929 未加载
评论 #31405802 未加载
评论 #31406182 未加载
评论 #31408632 未加载
patrckabout 3 years ago
I guess CCAN died?<p><a href="https:&#x2F;&#x2F;ccodearchive.net&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ccodearchive.net&#x2F;</a>
评论 #31405389 未加载
DeathArrowabout 3 years ago
What I find frustrating when I use C instead of C# is that I have to hunt for libraries and include them in the project or write my own implementation, even for most popular things like data structures, search algorithms, sorting algorithms, serialization, http calls. Whereas in C# the framework will provide them for me. If something is not in the standard library, I can use a directive or just reference a method from a package and the IDE will help to install the package and reference it in the project.<p>Go and Rust are similar in that aspect.
评论 #31407307 未加载
评论 #31406181 未加载
mikewarotabout 3 years ago
I really like the count prefixed strings, and will be adding it to my grow set of C stuff I&#x27;m gathering. My ultimate goal is to have something that does reference counted, count prefixed, null terminated strings, like Free Pascal.<p>I think using the cleanup attribute, and a defer macro can make it happen.
bjourneabout 3 years ago
I don&#x27;t understand why so many functions are implemented as macros when C99 has the inline keyword.
Upvoter33about 3 years ago
Interesting to look through.<p>I quickly scanned for things that stood out for me. As with anything, there are some things I disagree with.<p>To wit: the condition variable code includes a mutex inside of it, to deal with the case where you perform a signal on something before there is a waiter. Does this solve some problems? Sure. But it introduces extra overhead when I know what I&#x27;m doing and just want a condition variable.<p>These types of small things - the author thinks this is the &quot;right thing&quot;, whereas others won&#x27;t - are why no C standard C library like this exists. In C, the beauty is that you make every single decision; someone else&#x27;s decisions won&#x27;t be the same as yours.
评论 #31411592 未加载
bowsamicabout 3 years ago
I don’t understand how after all these years there isn’t a common library that everyone uses for this stuff. There seems to be a bunch of different ones, but not a single standout that most people use. Why not? Where is Boost for C?
评论 #31407904 未加载
numlock86about 3 years ago
Is there something like this but free of allocations at runtime? Things like a queue for example with a preset&#x2F;defined maximum size of entries that won&#x27;t suddenly grow but rather tell you that it&#x27;s full. I made an allocation free JSON parser&#x2F;serializer once but I don&#x27;t want to reinvent the wheel all the time ... Embedded devices with low RAM are still a thing and allocation free code is often mandatory.
kazinatorabout 3 years ago
My old kazlib has the basics: hashing, red-black-tree, lists, exception handling.<p><a href="https:&#x2F;&#x2F;www.kylheku.com&#x2F;~kaz&#x2F;kazlib.html" rel="nofollow">https:&#x2F;&#x2F;www.kylheku.com&#x2F;~kaz&#x2F;kazlib.html</a><p>Used in e2fsck, Ethereal and others.<p>There is even C++ support hidden in there: the dict.h contains a C++ template wrapper (which keeps the container intrusive).
lloydatkinsonabout 3 years ago
Seems to be mostly aimed at desktop OSs from a look at the source. How about one that’s platform independent, for embedded systems for example?
4dnightmareabout 3 years ago
Looks good
hypeateiabout 3 years ago
I&#x27;m not a C developer, nor have I ever been interested in developing with it.<p>From my perspective, it seems like a massive time drain and non-productive use of my time. Just a few points:<p>- Tooling seems all over the place (build system, package management)<p>- Having to roll your own trivial functions &#x2F; types (tooling may play into this)<p>- Versioning is confusing (C99, C11, ???)<p>The only advantage I see would be in embedded software because C is supposedly supported on a lot of platforms and is performant. But, I&#x27;m not actually sure this is true in practice.<p>Can you write one file of C code and compile it easily for multiple platforms or is there a lot of caveats?
评论 #31405917 未加载
评论 #31405258 未加载
评论 #31405232 未加载
评论 #31407019 未加载
评论 #31405437 未加载
评论 #31405567 未加载
评论 #31405270 未加载
sydthrowawayabout 3 years ago
Why not use GObject?
评论 #31404648 未加载
评论 #31404722 未加载
评论 #31404855 未加载
评论 #31408923 未加载
评论 #31406143 未加载