r/cprogramming • u/Xaneris47 • Aug 17 '26
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
212
Upvotes
r/cprogramming • u/Xaneris47 • Aug 17 '26
View all comments
Show parent comment
12
u/WittyStick Aug 17 '26
strlen is O(n).
For many string operations, we need the length to allocate the right amount of space, else we end up having to
reallocif our buffer isn't large enough - realloc is also O(n).By having a constant time length we can speed up a lot of string operations. It costs basically nothing to keep the length around rather than recomputing it each time.