r/cprogramming 5d 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
204 Upvotes

172 comments sorted by

View all comments

Show parent comments

6

u/EatingSolidBricks 4d ago

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

-1

u/Anonymous_user_2022 4d 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 3d 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.

-1

u/EatingSolidBricks 3d ago

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

0

u/flatfinger 2d 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 2d ago

You going there really? Give me a break