9 ms·
> just that unbeatable-ly fast Counterpoints: 1. 0-terminated C strings are slow. There's the constant need to scan the string to determine its length. Taking
by WalterBright 4y ago
> just that unbeatable-ly fast
Counterpoints:
1. 0-terminated C strings are slow. There's the constant need to scan the string to determine its length. Taking substrings requires making a copy. Yes, you can manually do length delineated strings in C, but there's no support for it in the language, it's error-prone, and doesn't interface with anybody else's C code.
2. One thing I've noticed over decades of using C is that it's very brittle. What that means is, once an algorithm is selected, it is never changed because it's too hard to refactor the code. (For example, changing a reference type to a value type means changing all the -> operators to .) This means C programs tend to get stuck in a local optimum of inefficient data structures and algorithms.
3. No array bounds checking, leading to a lot of time lost debugging the #1 programming bug in shipped C programs.
If you're doing RAII in C, you're more than ready to move to a more powerful language.
- pipeline_peak 4y ago> 0-terminated C strings are slow. There's the constant need to scan the string to determine its length. Just record it once
- WalterBright 4y agoRecord it where? Hence lie the bugs as C doesn't offer a reliable way to associate the two. I made a proposal to fix this in C, but it went nowhere: https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
- pipeline_peak 4y agoNot trying to be a troll, but is this benefit anything more than having to carry two variables? As in, is this about getting the length without having to use str Len even once?
- WalterBright 4y agoIf it's part of the type, then the compiler can automatically insert bounds checks. If not, then you'll have to insert the checks manually, which we both know won't happen. Another trouble with two variables is there's no obvious connection between them. One can be modified without the other, etc.
- nomel 4y agoObviously, a struct is one option. The problem is that you then need to make a whole string library. Thankfully, many exist, so this is a non issue, unless there's a requirement that you write all the code yourself.
- WalterBright 4y agoA struct won't be recognized by the language in order to automatically insert array bounds checks. > Thankfully, many exist, so this is a non issue Many string libraries that are incompatible with each other. This is a huge issue. (I myself made many C string libraries. It's not so easy. Try it.) The language extension I proposed for C is the same one D uses. D has had it for 20+ years, and it has proven very, very satisfactory.
- deleted 4y ago[deleted]
- nomel 4y ago> A struct won't be recognized by the language in order to automatically insert array bounds checks. If you want automatic bounds checks, then C is not the language for you. > This is a huge issue. Could you give an example where it's a huge problem? I'm probably limited by my experience. All of the codebases I've worked on used a single string library. When passing externally/to other libraries, boring C interfaces were used, then those libraries do what they wish from there. The string libraries I've used were mostly, deep down, just structs, with a length member, char pointer member, and encoding stuffs. Passing to the other library almost always ended up just being those member values being passed as arguments to a function, which were copied to the nearly identical structs of the other string library.
- WalterBright 4y agoSure. The C Standard library relies on 0 terminated strings. The Linux API relies on 0 terminated strings. Every C library I've every used relied on 0 terminated strings for its interface. So you use a translation layer. Sorry, I just don't like them, but if you're fine putting these on all your interfaces with other libraries, well, what can I say? :-) > If you want automatic bounds checks, then C is not the language for you. I would reframe that as: "if you're ok with buffer overflow malware injection, then C is the language for you!" Nobody has yet figured out how to stop that. The sad thing is it's so fixable with just a minor, compatible change to C.
- tomcam 4y agoOK you've done a bunch of real-world projects. Here's what I never understood. Why didn't some pseudo-standard for strings develop where they created a string-like type that remained null terminated but also included a big int at the beginning with a length value?
- jagged-chisel 4y agoBigint? How often are we dealing with strings over 4GiB?
- tomcam 4y agoIt would sure open up possibilities. Besides, I was like to plan ahead ;)
- synergy20 4y agoAll good points, which are part of the reasons I used other languages, but I'm back to C after weighing in those pros and cons. There is no perfect language, C just seems the best for me to get job done.
- WalterBright 4y agoAs long as one understands the tradeoffs, no problem.
- pitched 4y agoI was under the impression that C strings were inherently very fast because of how incredibly cache-friendly they are. You should not ever have to count all the bytes (memoize that!) but if you do, this is the best format there is for it.
- Thiez 4y agoWhat makes you say that? Besides C strings (pointer to characters, null-terminated) we have Pascal strings (pointer to length, followed by the characters), and then there is the variant that I'm not sure what it's called, that consists of a pair of (pointer, length). The last variant is my favorite, an is much nicer to use: you can take substrings without making a copy, you never have to iterate a string to find out its length, and your strings can contain the NULL character (which happens to be a perfectly normal unicode character).
- WalterBright 4y agoI'm not sure what it's called In D we call them dynamic arrays. They could be called length-delineated strings.
- Thiez 4y agoIt seems that Wikipedia refers to them as "Strings as records"[1], which I suppose fits quite well. I suppose that C strings could be considered "the most" cache friendly since it has only 1 byte overhead instead of however many bytes would be required for the length (probably 4 or 8 bytes on most modern systems). That said, I would be extremely surprised for this difference of a few bytes per string to have a measurable performance improvement in anything but an unrealistic microbenchmark involving many tiny strings. 1: https://en.wikipedia.org/wiki/String_(computer_science)#Strings_as_records https://en.wikipedia.org/wiki/String_(computer_science)#Stri...
- WalterBright 4y ago0 terminated strings make sense for extremely tight memory cases, like 64Kb computers, and CPUs that don't cache memory. But they're a clear loser for 32 bit machines.
- IChooseY0u 4y agoThere is nothing stopping you from storing size of the string. See: UNICODE_STRING This is a silly argument.
- kcexn 4y agoC is designed with a computing model in mind (the way memory works, the way arithmetic operations work, the way addressing works etc.). And its language idioms are designed to fall strictly in line with that computing model. There's very little difference in C between a character string, an array of Bytes, or even a struct of appropriate size. Other than the types and other user friendly (relative to assembly) features that C adds. This is deliberate; yep, it makes working with text harder (and maybe slower) than is necessary, but C doesn't assume that text is something you ever need to represent in your programs by default. So if you need to work with a lot of text, you should find a library for it, or write a library for it. C isn't designed to be fast (though it often is). It's not designed to be safe (though it also often is). It's designed to be extremely precise, like a hardware description, or a good maths paper. Some people like that precision. More often, people need that level of precision (see Linus' comments about C over C++ in the Linux kernel).
- howinteresting 4y ago> There's very little difference in C between a character string, an array of Bytes, or even a struct of appropriate size. This is really bad! > It's designed to be extremely precise, like a hardware description. This is absolute nonsense. There's far too much undefined behavior in C for it to be described as precise with a straight face. More generally, C's lack of memory safety has been directly responsible for innumerable vulnerabilities and trillions of dollars in costs to society. It is unacceptable and irresponsible to start a new project that's meant for public use in C in 2022.
- synergy20 4y agolinux,openbsd,systemd etc are all in c,c looks ok to me today.
- howinteresting 4y agoAll of them have had massive security vulnerabilities, and almost certainly (at least Linux and systemd) have many publicly undiscovered ones that are currently being exploited by state actors today.
- up2isomorphism 4y agoNull terminated string are never meant to use for large buffers. For all small strings that just locally it serves its purpose nicely. C designer realized that there is no way to have a standard way to deal with large performance-critical buffers , so they choose not to do it. Which is exactly the reason why C is so powerful - not necessarily fast but never get in your way if you want to. For array bounds checking, if you want it, just a write a structure with buffer length and a getter setter and it is done. People complain this write zillions of getter setters in other languages but just choose to complain in case for C.
- WalterBright 4y ago> For array bounds checking, if you want it, just a write a structure with buffer length and a getter setter and it is done. It's so easy, yet buffer overflows remain the #1 problem in shipped C code. > For all small strings that just locally it serves its purpose nicely. Not really. Whenever I review other peoples' C code, I look at their use of strlen/strncpy/strxxx functions. They're a rich source of bugs, and I'll usually find one in it (usually an off-by-one error). They don't have to be large strings, either, to be slow.
- up2isomorphism 4y agoIt just means not that many people give a damn about buffer overflows anyway. And put that situation to language’s fault just doesn’t make sense. You are saying C string is slow, I am telling you short local strings are not slow, please tell me why it is slow in that case?