4 ms·
C strings do not have a cached length. This is one of their largest weaknesses, which is why many other languages do not use C strings.
by steveklabnik 8y ago
C strings do not have a cached length.
This is one of their largest weaknesses, which is why many other languages do not use C strings.
- fixermark 8y agoOh. Right. I think I just gave away the game on how long it's been since I programmed in C itself. Even C++ has "free" length() in std::string (in the sense that it's specified that it must be a constant-time operation). ... that's actually a good example of the trap of thinking of C as "what's really going on." If you assume C's treatment of char* is how strings have to be done, you're out of alignment with basically all other programming languages and you might be writing unnecessarily slow code (or forcing developers using your code to juggle a homegrown "length" abstraction that the underlying language should be juggling for them).