4 ms·
I agree, but C (the language) doesn't even have the concept of a 'string'. It's just the convention how some C standard library functions interpret an array of
by flohofwoe 8y ago
I agree, but C (the language) doesn't even have the concept of a 'string'. It's just the convention how some C standard library functions interpret an array of bytes with a zero at the end.
At least in C it's quite obvious that strings are not trivial if you want both an intuitive way to work with strings, and high performance. The C++ std::string type is neither intuitive to work with, nor does it allow to write high-performance code.
For string processing it's really better to use another language with different trade-offs.
- pjmlp 8y agoC++ std::string is better and more secure and anything that C ever produced. As for string processing in general, I do agree that other languages are better suited.
- aap_ 8y agoMore often that not I find myself missing C-type strings in other languages. Being able to just walk through the characters and manipulating them is something I found rather ugly in python for instance. The NUL character is in my experience not so terrible, you typically have null pointers at the end of a linked list or whatever as well and nobody complains about that. Now I have to admit I had a bug recently that took me longer to fix then I would like to admit because I wasn't walking a string right, but usually I have very little trouble with them.
- adrianN 8y agoWhat's a character? UTF-8 makes that a bit difficult to answer. If you want arrays of ASCII bytes, you can have those in most programming languages.
- lugg 8y ago1 to 4 bytes. How does utf8 make that difficult to answer? When was the last time you iterated over a string of unicode points and said you know what would be handy right now? If these code points were split up into arbitrary and unusable bytes of memory.
- adrianN 8y agoWell, ä is a character in German. You can either write it as LATIN SMALL LETTER A WITH DIAERESIS, or you can use COMBINING DIAERESIS and a. When you iterate over the German word Mädchen as Unicode code points you might be confused. Other languages do much crazier things.
- lugg 8y agoThat doesn't make it hard..
- olau 8y agoIt's been a long time since I wrote C, but the main problem in my recollection is that the standard library is not intuitive. Something simple like take a couple of arbitrary strings, concatenate them and return the result without leaking memory and not causing buffer overflows is not as trivial as it should be. I don't think is a huge problem per se, though, you can just use a string library. See the confusion here for an example: https://stackoverflow.com/questions/308695/how-do-i-concatenate-const-literal-strings-in-c https://stackoverflow.com/questions/308695/how-do-i-concaten... snprintf looks like it is the easiest way out.
- coldtea 8y ago>More often that not I find myself missing C-type strings in other languages. Being able to just walk through the characters and manipulating them is something I found rather ugly in python for instance If what you say if you want mutable strings, many languages have those and you don't need anything like "NULL" to have them (and you can use a bytearray of the string in Python, though Unicode complicates this). >The NUL character is in my experience not so terrible, you typically have null pointers at the end of a linked list or whatever as well and nobody complains about that That's not the same thing at all. The linked list is comprised of structs with next fields, that can be null or point to something. Your program can handle either just fine, as both are valid cases (a linked list expects to find the NULL guard at the end but also expects a non-NULL next pointer if the node is not the last one, so will handle both). OTOH, if an incoming string doesn't have a NUL byte your program will crash/corrupt memory/worse. On top of that, you need to remember it to add it/make space/for most string manipulations. Strings are not expected NOT to end with NUL, and when they don't there's no way you can mitigate it, except to set arbitrary limits to how many characters you consider.
- leetcrew 8y ago> OTOH, if an incoming string doesn't have a NUL byte your program will crash/corrupt memory/worse. how are linked lists different? if the last node contains garbage for its next pointer, the outcome will be exactly the same. it's a bit more rare to encounter an "unterminated" linked list, but I've seen it happen plenty of times deserializing a linked list from disk or if the programmer just forgot to initialize the pointer. c strings basically are linked lists with an implicit next pointer.
- atilaneves 8y ago> I agree, but C (the language) doesn't even have the concept of a 'string' It has string literals, so yes it does. > The C++ std::string type is neither intuitive to work with Many would disagree. > nor does it allow to write high-performance code True, but only due to backwards compatibility with C - std::string operations have to add a null terminator for no other good reason.
- badsectoracula 8y ago> Many would disagree. Many would also agree, that means nothing. Personally i dislike C++'s strings... and the rest of STL, which i view as one of the worst standard library APIs in wide use.
- jstimpfle 8y ago> True, but only due to backwards compatibility with C - std::string operations have to add a null terminator for no other good reason. That's about the least reason why std::string is inefficient.
- sureaboutthis 8y agoThis is why I don't like other languages. They have no concept of functions to interpret an array of bytes with a zero at the end.
- coldtea 8y agoActually (and I know this is a lame troll), it's trivial to make such functions in most non-C languages.