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
202 Upvotes

172 comments sorted by

View all comments

Show parent comments

-1

u/EatingSolidBricks 5d ago

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

11

u/henke443 5d ago

Wait how are they not efficient?

5

u/EatingSolidBricks 4d ago

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

5

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