SDS
pottery
SDS | pottery | |
---|---|---|
48 | 14 | |
4,803 | 119 | |
- | - | |
0.0 | 1.8 | |
7 months ago | about 2 years ago | |
C | C | |
BSD 2-clause "Simplified" License | MIT License |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
SDS
-
Strlcpy and strlcat added to glibc 2.38
Let me reframe this. What we're saying to do is stop using C string manipulation such as strcat, strcpy, etc. Particularly, I'm saying simply don't use C-style null terminated strings until you actually go to call a C ABI interface where it is necessary.
The argument against this is that you might call something that already does this. Yes, sure, that IS true, but what this betrays is the fact that you have to deal with that regardless of whether or not you add additional error-prone C string manipulation code on top of having to worry about memory ownership, mutation, etc. when passing blobs of memory to "untrusted" APIs.
It's not about passing the buck. Passing a blob of memory to an API that might do horrible things not defined by an API contract is not safe if you do strcat to construct the string or you clone it out of an std::string or you marshal it from Go or Rust. It's about not creating a bigger mess than you already have.
Okay fine, but what if someone hates C++ and Rust and Go and Zig? No problem. There are a slew of options for C that can all handle safer, less error-prone string manipulation, including interoperability with null-terminated C strings. Like this one used in Redis:
https://github.com/antirez/sds
And on top of everything else, it's quite ergonomic, so it seems silly to not consider it.
This entire line of thinking deeply reminds me of Technology Connection's video The LED Traffic Light and the Danger of "But Sometimes!".
https://youtube.com/watch?v=GiYO1TObNz8
I think hypothetically you can construct some scenarios where not using C strings for string manipulation requires more care, but justifying error prone C string manipulation with "well, I might call something that might do something unreasonable" as if that isn't still your problem regardless of how you get there makes zero sense to me.
And besides, these hypothetical incorrect APIs would crash horrifically on the DS9K anyways.
-
Safest way to copy a string?
Even better, use a string handling library. Personally I am a big fan of (sds)[https://github.com/antirez/sds] from the Redis creator. It's not even a dependancy you can just copy the .c and .h to your project.
-
New C features in GCC 13
One nice application is length-prefixed string literals to complement dynamic string libraries:
-
Strlcpy and Strlcat – Consistent, Safe, String Copy and Concatenation (1999) [pdf]
The better answer would be to add data types like SDS[0] to the standard library, and use them as ADTs (Abstract Data Types) [1].
Unfortunely WG14 has proven in 30 years of existence, that it isn't something that they care to fix, and while 3rd party solutions exist, without vocabulary types on the stardard library adoption will never take off.
[0] - https://github.com/antirez/sds
[1] - https://en.wikipedia.org/wiki/Abstract_data_type
-
C Strings and my slow descent to madness
With the woes of string.h being known, why not just use an alternative like https://github.com/antirez/sds ?
I’ve also been having a blast with C because writing C feels like being a god! But the biggest thing that I like about C is that the world is sort of written on it!
Just yesterday I needed to parse a JSON… found a bunch of libraries that do that and just picked one that I liked the API.
-
How can i know for sure that i am allocating enough memory?
Please note that the discussion started with requirement for no dynamic allocation in critical code what virtually eliminates std::string. I agree that std::string code tends to be simpler but the main reason is that the standard C library sucks on strings. There are better alternatives like sds but they are ... not standard.
-
str_header.h - A single header C string library
Another day, another post about a writing a bespoke string lib instead of using SDS
-
C_dictionary: A simple dynamically typed and sized hashmap in C - feedback welcome
d) everything being a macro seems overkill for me (and possibly dangerous, see b)). Maybe implement more as static inline functions, see the sds header: https://github.com/antirez/sds/blob/master/sds.h (which does a similar thing with the header struct).
-
Updated book to learn C
For example, you can use the C language with sds strings (see https://github.com/antirez/sds) if you want to have an easier time with string formatting and don't want to worry about using the famously unsafe string.h functions correctly. You'll still program in ISO C, but just not in the standard library. The same applies to pretty much all parts of the standard library, the only part unsurpassed is pretty much just printf and the math headers (math.h, fenv.h, tgmath.h, complex.h) imo, and the occasional call to exit. A good place to look for libraries if you want to go that route is the awesome-c collection: https://github.com/oz123/awesome-c
-
Convenient Containers: A usability-oriented generic container library
One way around this problem is to declare the container as a pointer to the element type and then store the container’s metadata, alongside its elements, in the heap block to which the pointer points. This approach is already used for dynamic arrays in several container libraries, most notably stb_ds and sds. They place the metadata before the elements and provide the user with a pointer to the elements themselves (this has the nice effect that users can use the [] operator to access elements).
pottery
-
Popular Data Structure Libraries in C ?
Pottery - The page for open hash map reads "Documentation still needs to be written. In the meantime check out the examples."
-
So what's the best data structures and algorithms library for C?
"Using macros" is a broad description that covers multiple paradigms. There are libraries that use macros in combination with typed pointers and functions that take void* parameters to provide some degree of API genericity and type safety at the same time (e.g. stb_ds and, as you mentioned, my own CC). There are libraries that use macros (or #include directives) to manually instantiate templates (e.g. STC, M*LIB, and Pottery). And then there are libraries that are implemented entirely or almost entirely as macros (e.g. uthash).
-
Better C Generics: The Extendible _Generic
The prototype of CC used this mechanism to provide a generic API for types instantiated via templates (so basically like other container libraries, but with an extendible-_Generic-based API laid over the top of the generated types). This approach has some significant advantages over the approach CC now uses, but I got a bit obsessed with eliminating the need to manually instantiate templates.
- C_dictionary: A simple dynamically typed and sized hashmap in C - feedback welcome
-
Common libraries and data structures for C
I think it's common for C programmers to roll their own. I did the same [0].
I went pretty deep into composable C templates to build mine so it's more powerful than most. The containers can handle non-bitwise-movable types with full C++-style lifecycle functions and such, and the sort algorithms can handle dynamic and non-contiguous arrays (they are powerful enough to implement qsort() [1], which is more than I can say for any other C sort templates I've seen.) My reasoning for the complexity at the time was that any powerful container library is going to be reasonably complex in implementation (as anyone who's looked at STL source code knows), so it just needs to be encapsulated behind a good interface.
I'm not so sure that's true anymore. These sorts of simpler libraries like the one linked here definitely seem to be more popular among C programmers. I think if people are using C, it's not just the C++ language complexity they want to get away from, but also the implementation complexity of libraries and such. There's a balance to be had for sure, and I think the balance varies from person to person, which is why no library has emerged as the de facto standard for containers in C.
[0]: https://github.com/ludocode/pottery
- C++ containers but in C
- Pottery – A pure C, include-only, type-safe, algorithm template library
- Ask HN: What you up to? (Who doesn't want to be hired?)
-
Type-safe generic data structures in C
Yes! The include style of templates in C is way better than the old way of huge macros to instantiate code. The template code can look mostly like idiomatic C, it interacts way better with a debugger, it gives better compiler errors... everything about it is better and it's finally starting to become more popular.
I've open sourced my own C template library here:
https://github.com/ludocode/pottery
Not only does it use the #include style of templates, but it actually makes the templates composable. It takes this idea pretty far, for example having a lifecycle template that lets you define operations on your type like move, copy, destroy, etc. This way the containers can fully manage the lifecycles of your types even if they're not bitwise movable.
There's also this other more popular C template library, one that tries to more directly port C++ templates to C but with a lot less features:
https://github.com/glouw/ctl/
-
Beating Up on Qsort (2019)
This article doesn't really make it clear but the merge sort discussion is specifically about glibc's implementation of qsort(). glibc's qsort() and Wine's qsort() are the only ones I know of that use merge sort to implement qsort(). Most implementations use quick sort.
I recently did my own benchmarking on various qsort()s since I was trying to implement a faster one. The various BSDs and macOS qsort() are all faster than glibc at sorting integers and they don't allocate memory:
https://github.com/ludocode/pottery/tree/master/examples/pot...
Of course sorting is much faster if you can inline the comparator so a templated sort algorithm is always going to be faster than a function that takes a function pointer. But this does not require C++; it can be done in plain C. The templated intro_sort from Pottery (linked above) is competitive with std::sort, as are the excellent swensort/sort templates:
https://github.com/swenson/sort
What are some alternatives?
Better String - The Better String Library
mpack - MPack - A C encoder/decoder for the MessagePack serialization format / msgpack.org[C]
Experimental Boost.MSM-lite - Boost.SML (formerly called Boost.MSM-lite)
pdqsort - Pattern-defeating quicksort.
libcpuid - a small C library for x86 CPU detection and feature extraction
mavis - opinionated typing library for elixir
ZXing - ZXing ("Zebra Crossing") barcode scanning library for Java, Android
sc - Common libraries and data structures for C.
safestringlib
Klib - A standalone and lightweight C library
stb - stb single-file public domain libraries for C/C++
ctl - My variant of the C Template Library