Does wstring interrupt null?

What is the internal structure of std :: wstring? Does it include length? Does it end in zero? Both?

+7
c ++
source share
3 answers

Does it include length

Yes. This is required by the C ++ 11 standard.

§ 21.4.4

size_type size() const noexcept;
1. Returns: the number of char objects currently in the string. 2. Difficulty: constant time.

Please note, however, that this is not known about unicode.


Does it end in zero

Yes. The C ++ 11 standard also requires std::basic_string::c_str return a valid pointer for the range [0, size ()], in which my_string[my_string.size()] will be valid, therefore it will have a null character.

§ 21.4.7.1

const charT* c_str() const noexcept;
const charT* data() const noexcept;
1. Returns: a pointer p such that p + i == &operator[](i) for every i in [0,size()] .
2. Difficulty: constant time.
3. Required: the program must not change any of the values ​​stored in the character array.

+12
source share

We do not know. This is completely up to implementation. (At least until C ++ 03 - apparently C ++ 11 requires the internal buffer to end with 0.) You can look at the source code for the implementation of the standard C ++ library if the one you are using is open source.


In addition, I would consider it logical if it was completed with NUL and , and it retained an explicit length. This is good, because then it takes constant time to return the length and valid string of C:

 size_t length() { return m_length; } const wchar_t *c_str() { return m_cstr; } 

If it did not preserve the explicit length, then size() would have to count the characters before NUL in O(n) , which is wasteful if you can avoid it.

If, however, the internal buffer was not NUL-terminated, but it only kept the length, then it would be tedious to create a valid C line with zero completion: the line should either redistribute its storage or add 0 (and redistributing is an expensive operation), or will have to copy the entire buffer, which again is O(n) operation.

(Warning: shameless self-promotion - in a C project that I am currently working on, I used this approach to implement flexible string objects.)

+10
source share

basic_string (of which wstring is a typedef) does not need terminators.

Yes, he controls his own length.

If you need a string with string / wstring with a null terminating string (aka C string), call c_str (). But it can contain a null character inside it, in which case almost every C function to process C strings will not be able to see the entire string.

0
source share

All Articles