Why does a C++ string end in a null terminator? [duplicate]
Possible Duplicate:
What's the rationale for null terminated strings?
My question is very simple: why do C++ strings end in null terminators? Is this so that you do not need to keep count on the length?
Traditional strings in C and C++ use a null terminator to indicate the end of the string. Since string pointers simply pointed to an array of characters, without any length or other meta data, the null terminator was the only way to determine the length of the string.
As far as why it was done this way, that's more difficult question to answer. There are many ways to store string data, that's just one of them.
There are two basic techniques to store raw strings, one which stores the length in the header and the other one which uses zero terminator for the ending.
The designerns of the C language choose the latter one. That's why. C++ just inherited this, and std::string
uses this technique so c_str()
is O(1). note: I don't know whether the standard requires this and not even care.
A string can be thought of a variable length field. Keeping the count somewhere will need a certain format, like for example first the length is stored and then the string (like frame headers in ID3v2 tags). Keeping a terminator character to mark the end of the variable length field is another method. C has choose this way to mark the termination of the string field. Depends on how we interpret the sequence of bits and bytes.
Not an official answer but I can reckon the reason behind C (and later C++) having strings that end in zero is because it requires only one machine instruction to branch if the value loaded into memory is zero (the null terminator). That, and it's only one byte, as opposed to Pascal strings that are limited to 255 characters without adding another byte.
精彩评论