Is there a terminal character at the end of strings?
2 answers
Yes.
Or, at least, a place is reserved for him and its value is forged. This would be irrational, but not prohibited, and therefore may be the case.
With C ++ 11, this is guaranteed, because the c_str
method of std::string
should (a) work in constant time and (b) return a pointer directly to the internal buffer of the string object.
This is the terminating numeric character ('\ 0') at the end.
Although when working with std::string
(and not with its C-compatible piece), the terminal symbol may not play its role , and we can assume that it does not exist.
It is terminal only for the algorithms that use this string.
- Yeah, everything flows, everything changes. I remember 5 years ago, here (on the HC), they argued with foaming at my mouth (poking my nose at the same stupid standard and disagreeing with the logic of the possible implementation) that it is not safe to pass an address from the middle of stish :: string to Sishnye functions, since the standard does not guarantees zero at the end ... - avp
- 2@avp, Standard is not stupid. Simply: a) it is not perfect; b) develops. But it is better to follow the letter of the law (Standard), rather than rely on implementation, which may change when switching to another system in the most unexpected at first glance places. - 伪位蔚蠂慰位蠀蟿
- @alexolut, or doing implementations that change standards :-). - avp
- one@avp: passing the address from the middle of std :: string to functions is still insecure: in the middle of std :: string it can be \ 0, unlike the string. - VladD
- one@VladD, of course, misunderstanding with zeros in the string is quite possible. The impossibility of storing zeros in sish lines can sometimes be overcome in a rather non-obvious way. For example, if a text editor stores a file in rows, then it is obvious that there are no
\n
-s in them. And then you can replace the zeros on them, and when doing output to the file, do a reverse replacement (of course, when outputting to the screen, you also need a separate check to print, say,C-@
) - avp
The standard guarantees that if the std::string
queried for the last character in one way or another (for example, using operator[]
with size()
argument), then a character similar to that constructed by calling char()
will be returned, i.e. 0
So we can say that 0
exists at the end of the line.
At the same time, there is still some inconsistency in the Standard. For example, the description of the constructor, which is called by the default constructor, says that:
The
data()
member function, after initializing the object, returns a non-null pointer, which can contain 0.
Those. can, but not required. At the same time, for the value obtained through data()
following equality is true:
p + i == &operator[](i)
, for all i in the range [0, size ()]
And given the work of operator[]
, which is stated at the very beginning of my answer, it turns out that 0
is still not just able, but obliged to exist in memory.
std::string
. - VladD