News for hackers. Stuff that matters.
Re:C Strings and my slow descent to madness by _benj on Thursday April 06, 2023 @09:27AM (#35468039)
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.

Re:C Strings and my slow descent to madness by t43562 on Thursday April 06, 2023 @09:39AM (#35468166)
Yes, I do think that you cannot fairly dismiss C because of strcpy_l only being available on Windows when it's quite possible to implement it yourself or use a library like the one you mention.
Re:C Strings and my slow descent to madness by felipemnoa on Thursday April 06, 2023 @09:36AM (#35468132)
>>I’ve also been having a blast with C because writing C feels like being a god

Not trying to be a troll but as someone who has also written a lot of C in the past why do you feel like this?

Re:C Strings and my slow descent to madness by t43562 on Thursday April 06, 2023 @09:40AM (#35468177)
It's not doing as many things behind your back as the dynamic languages and C++ do. More things are your responsibility.
Re:C Strings and my slow descent to madness by _benj on Thursday April 06, 2023 @09:57AM (#35468376)
It’s the access and control that it gives me!

As when I’d pick Go because I was doing some concurrency, I can now explore a bunch of concurrency libraries, including some implementations that look a lot like Go channels. Want to watch a file for changes? I can do that all the way from taking to the kernel to picking a multi-platform library. I guess, I haven’t really found anything that I can’t do in C, and if I’m lazy, I can just embed and scripting language to handle things in a higher level. Macros are also very powerful! I’ve been writing code that writes code for me, export the thing to a .h file and import it using #include.

pkg-config —list-all has become my friend and I keep discovering that the world is written in C and the access to libraries is huge!

Also, idiomatic C is whatever you make it. There are a bunch of ways to skin a cat. Want a different platform? Build tool? Compiler? Debugger? Wanna write you own debugger? C is chill with that.

It’s also such a simple language that without much effort you can know everything about it (I don’t care that much about anything over C99). I don’t know the whole ecosystem, or standard libraries, or data structures and algorithms and whatnot, but the language itself is quite trivial.

With that said, I’m not using C in project teams. In that setting some strong conventions would likely be necessary or even better, something enforced by tools or the compiler (like Go), but yeah, I’ve been quite enjoying working with C and being kind of annoyed at other langs that I need to use for work because they keep doing all this stuff behind my back that is supposed to help me, but instead is a pain trying to debug and understand what is actually happening

Re:C Strings and my slow descent to madness by felipemnoa on Thursday April 06, 2023 @11:15AM (#35469465)
Thank You! That was a nice explanation.
Re:C Strings and my slow descent to madness by Georgelemental on Thursday April 06, 2023 @02:29PM (#35472413)
Sorry to be "that person", but have you tried Rust yet? It checks a lot of your boxes:

- Access and control, nothing "behind your back"

- Low or high level, as you prefer

- Swap in different implementations (custom allocator, different async runtime, etc)

- Really powerful macros

- Strong conventions, safe by default (but you can break them, go into the weeds if needed)

Downsides compared to your list:

- More complex than C or Go (though less than C++)

- Only one production compiler, and everyone assumes `cargo` build system (though both are very good)

- Library ecosystem not quite as extensive (though there is a lot of good stuff on crates.io, and you can always write bindings to C)

Re:C Strings and my slow descent to madness by _benj on Thursday April 06, 2023 @05:20PM (#35474637)
hehe! It's ok to be "that person" :)

Yes, I've tried rust and have even shipped some project with it! I think the things that didn't worked for me was the complexity. It felt like I had to keep a lot of things in mind to be effective, (traits can be a bit obscure, i.e. magic IMHO), also lifetimes and Option made the code complex by either having a bunch of math or .unwrap all over the place.

With that said tho, Rust would be one of my top picks for a professional setting or a codebase that I share with a team, because of the really good defaults that it has! I read once somebody comparing the Rust compiler with a bunch of tiny unit tests that the developer doesn't have to write, and I agree with that!

With C tho, for personal stuff I can do things that I've never do professionally (i.e. use the OS as my GC because when the process dies the OS is gonna "free" my memory allocations anyways! I know, terrible, but I'm having fun! ¯\_(ツ)_/¯)

Re:C Strings and my slow descent to madness by kovac on Thursday April 06, 2023 @08:22PM (#35476439)
I came from HLL like C# and I've come to love C for the same reasons. The only language I can fit in my head in its entirety and no one telling me this is the "right way" to write code, don't use this or that feature, etc. When I'm writing C, I am free. I absolutely love this freedom!
Re:C Strings and my slow descent to madness by _benj on Friday April 07, 2023 @08:58AM (#35481112)
Exactly!
Re:C Strings and my slow descent to madness by gavinhoward on Thursday April 06, 2023 @09:47AM (#35468249)
I encourage people to use sds if that's the best option.

However, I don't think that's the best option if you can roll your own.

I personally think that there should be two different types of strings: static and dynamic ones. The static ones should not be able to be changed, but the dynamic ones can serve as a "string builder" type of sorts.

Second, I don't see sds's first advantage (in the README) to be an advantage. Sure, you may have to explicitly pass the buffer in to C functions, but that tells you that you're calling a function that takes a char array rather than your string. It makes it more explicit.

Second, if you use my method of splitting static strings from dynamic strings, then sds's second advantage doesn't apply because the pointer will never change.

But the disadvantages of sds still apply, and both disadvantages are big since they easily lead to bugs. Hence, I think sds is not the best option if you can make your own.

Oh, another advantage of the static/dynamic split: I can implement small string semantics. For small enough strings, I use a union to put the array into the same bytes as the pointer, so on 64-bit machines, I can have 8-byte strings (including nul) before needing an actual allocation.

Re:C Strings and my slow descent to madness by kerkeslager on Thursday April 06, 2023 @09:50AM (#35468291)
> With the woes of string.h being known, why not just use an alternative like https://github.com/antirez/sds ?

That library really doesn't address any of the Unicode issues.

Re:C Strings and my slow descent to madness by UncleEntity on Thursday April 06, 2023 @09:51AM (#35468301)
Was going to say the same thing.

If you want Unicode in C a wrapper library is pretty much a given.

When I was adding Unicode support to the small scheme interpreter I like playing with I found a super simple string library, a bunch of generated code (who doesn’t love 1000 line switch statements) for dealing with utf-8 code points and bob’s your uncle. Could have probably found a library that did it all but the goal was learning and yak shaving.

Haven’t ever messed with the wide strings, seems like more of a hassle than they’re worth.

Re:C Strings and my slow descent to madness by kelnos on Thursday April 06, 2023 @05:29PM (#35474756)
> I’ve also been having a blast with C because writing C feels like being a god!

That's funny, because I look at it in the opposite way: it makes me feel like a super-fallible human because it's so easy for me to break things in horrible ways, something a hypothetical god would not do.

Something like Rust &str or String would make me feel more like a god, as I can do whatever I want (more or less) without worrying about safety.

Re:C Strings and my slow descent to madness by lolcatuser on Thursday April 06, 2023 @08:31PM (#35476513)
A hypothetical god would not destroy the world, but absolutely could.

I think that's the idea - they're not reveling in the fact that they can do anything anybody could reasonably want to do, they're reveling in the fact that they can also do everything else, too.