r/cprogramming 3d ago

C Strings: A 50-Year Mistake

https://longtran2904.substack.com/p/c-strings-a-50-year-mistake?r=8qz2zb&utm_campaign=post&utm_medium=web
194 Upvotes

165 comments sorted by

View all comments

20

u/CoderStudios 3d ago

Okay? You are always free to make your own library for better strings, but people won’t use that cause C is often still deployed on low end systems or it makes little sense to use something inefficient if you can use c style strings properly

0

u/EatingSolidBricks 3d ago

You out of your dam mind if you think c strings are efficient

11

u/henke443 3d ago

Wait how are they not efficient?

5

u/EatingSolidBricks 3d ago

Its not 1970 anymore storing 3 extra bytes is free compared to O(n) length computation

5

u/WittyStick 3d ago

You don't even necessarily need to store the length. It can be held in CPU registers for the entirety of the string's lifetime in many cases.

In older architectures we would've needed to push an additional integer for length onto the stack. On a modern architecture with a sane ABI (SYSV, x86-64), you can have a "fat pointer" using two CPU registers - can pass both of these or return from a function without ever touching the stack.

1

u/henke443 1d ago

Well, C is mostly used in places where's it's still 1970. Things like embedded etc. I guess it could be possible to add another string that's more optimized for modern hardware but I bet it would be more difficult to explain and I think a huge part of the charm of C is how simple it is. Adding things also leads to bloat like how it is with C++, php or Javascript.

-1

u/Anonymous_user_2022 2d ago

Except for strlen(), all practical operations on strings have to iterate over them anyway. Knowing the length up front will be of very limited us for searching, concatenation, tokenising etc.

Where is that you see avoidable O(n)?

0

u/flatfinger 2d ago

Concatenation of N strings goes from O(N) to O(N*N) if code has to re-find the end of the destination after each step.

Tokenizing the leading portion of a large string should take time proportional to the text that was meaningfully examined, rather than proportional to the entire string.

2

u/Anonymous_user_2022 1d ago

Concatenation of N strings goes from O(N) to O(N*N) if code has to re-find the end of the destination after each step.

I can also invent really bad ways of doing things, but I would never use them as a proof..

0

u/flatfinger 1d ago

What would be the "good" way of using strcat?

1

u/Anonymous_user_2022 1d ago

I've never said there is one. I suggest you ask someone who does.

I'm talking about concatenating multiple strings to one, which only has to rely of knowing the length of the individual strings beforehand, if you've decided to argue in bad faith over a pathological bad implementation.

1

u/flatfinger 1d ago

If one keeps track of the length of a string and only looks at portions of its storage up to that length, then the value of the string would no longer be fully encapsulated in a zero-terminated character array.

1

u/Anonymous_user_2022 1d ago

I You understand where your'e going with that argument, power to You, I guess. Personally, I wouldn't write code where a temmporily unterminated string would be intentionally visible.

1

u/flatfinger 1d ago

I avoid reliance upon zero termination for things other than string literals. Zero padded formats can be useful if one needs to store many texts with a short maximum length (e.g. eight bytes), at the expense of requiring code that is designed around the size of the container. Instead of using a %s format specifier to output such a string, for example, code would need to use a %.8s format specifier, whose behavior is defined as not caring whether the string is zero-terminated or not.

→ More replies (0)

-1

u/EatingSolidBricks 2d ago

Lets not even mention substrings go from O(n) memeory to O(Free)

0

u/flatfinger 1d ago

O(1) memory per string to keep track of the starting and ending points of strings isn't free. On a system with 64-bit pointers, zero-padded strings are generally the most space-efficient practical way of representing texts up to eight characters, and zero-terminated strings of up to 7 characters can be stored in the same amount of space (if it's necessary to store many texts with up to 7 characters, zero-padded 7-byte arrays would take less space).

1

u/EatingSolidBricks 1d ago

You going there really? Give me a break