Live data from Hacker News

SDS: Simple Dynamic Strings library for C

github.com

21–30 of 48 posts

Re: SDS: Simple Dynamic Strings library for C

#21
post #2

Why not just use C++ strings and string_views? So weird to see this masochistic obsession some people have with doing everything in plain C. It's 2026, there are better, more memory safe, more efficient solutions out there.

Redis started in 2009, and this library was started there. string_view didn't appear until C++17.

Re: SDS: Simple Dynamic Strings library for C

#23
post #14
post #2

Why not just use C++ strings and string_views? So weird to see this masochistic obsession some people have with doing everything in plain C. It's 2026, there are better, more memory safe, more efficient solutions out there.

I switched to C because I could not stand the pain of using C++ anymore. I find C refreshingly simple. (Also, as a comment to other responses: C++ is not a superset of C, it is a fork from 95 with divergent language evolution since then).

It is not a fork from 95. Cfront, the original C++ compiler was a new front-end for the C compiler from 1983 that output C which was then compiled regularly.

https://en.wikipedia.org/wiki/Cfront

Re: SDS: Simple Dynamic Strings library for C

#24
post #19

Hi! The Redis tree contains more advanced versions of this library. Most of the development continued there, eventually.

Hi! Are there no performance penalties that come from alignment issues? Or are your prefix structs aligned to cache line sizes?

Re: SDS: Simple Dynamic Strings library for C

#26
post #14

Earlier quoted context omitted.

I switched to C because I could not stand the pain of using C++ anymore. I find C refreshingly simple. (Also, as a comment to other responses: C++ is not a superset of C, it is a fork from 95 with divergent language evolution since then).

It is not a fork from 95. Cfront, the original C++ compiler was a new front-end for the C compiler from 1983 that output C which was then compiled regularly. https://en.wikipedia.org/wiki/Cfront

Maybe, but it is 1995 when it diverged: https://isocpp.org/wiki/faq/c Before it was an extension since then it is a fork.

Re: SDS: Simple Dynamic Strings library for C

#28
post #19

Hi! The Redis tree contains more advanced versions of this library. Most of the development continued there, eventually.

It might be worth extracting it back out, it seems pretty useful.

Indeed, but in some way the Redis version is a bit too Redis-ish, that is, memory saving concerns are taken to the extreme instead of having a more balanced approach about simplicity. In my YouTube channel C course, I'm showing something similar to SDS in the latest lessons, and I may use SDS again in later course in order to show how to integrate back the useful features that diverged. Maybe an SDS3 maybe a middle ground among the Redis version, some API error that should be corrected (but not in Redis: not worth it), and other improvements.

Re: SDS: Simple Dynamic Strings library for C

#29
post #10
post #2

Why not just use C++ strings and string_views? So weird to see this masochistic obsession some people have with doing everything in plain C. It's 2026, there are better, more memory safe, more efficient solutions out there.

If your code is plain C, then anyone can extend it with, or embed it inside, code of literally any other language; and in so doing, they will have full access/exposure to everything in your codebase — all the same stuff that they would if they were writing their host/extension code in C. This is not true of C++ (or most other languages): • C++ has a runtime (however minimal); and so, by including any C++ code in a co…

C++ has a runtime (however minimal);

No it doesn't.

Also, even if there was no associated runtime to deal with, C++ isn't wholly C-FFI-clean

Yes it is, you just extern "C" whatever you want.

All the stuff that people like about C++ — all the reasons you'd want to use C++ — result in codebases that aren't cleanly C-FFI exposable

Not true at all, the biggest two things, destructors and move semantics you still have everywhere except for the boundaries with C.

And even if you bite that bullet, and write your library in C++ but carefully wrap its API to give it C-FFI-clean linkage (usually via a hybrid C / C++ project), this still introduces a layer of FFI runtime overhead

There is no overhead here, it is not different from C.

I don't know where all this comes from, but I doubt it comes from heavy experience with modern C++.

Re: SDS: Simple Dynamic Strings library for C

#30
post #17

I'm surprised this is aliased to char*, not const char*. The benefit of the aliasing is convenience, but the main risk is absent-mindedly passing it to a libc function that modifies the string without updating the SDS metadata. Const would result in a compiler warning while letting the intended use cases (e.g., the printf example) work fine.

The only thing the SDS metadata holds is the string's length. Just like how you'd have to realloc() a regular string before using strcat(), you have to sdsgrowzero() an sds string before using strcat(). Basically, standard libc functions that tamper with the string have the same constraints as malloc()ed strings in terms of safety, only you might want to call sdsupdatelen() after truncating a string.
Post reply on HN