Is wstring null terminated?

c++

Solution

Does it include the length

Yes. It's required by the C++11 standard.

§ 21.4.4

`size_type size() const noexcept;` 1. Returns: A count of the number of char-like objects currently in the string. 2. Complexity: constant time.

Note however, that this is unaware of unicode.

Is it null terminated

Yes. It's also required by the C++11 standard that `std::basic_string::c_str` returns a valid pointer for the range of [0,size()] in which `my_string[my_string.size()]` will be valid, hence 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 each `i` in `[0,size()]`. 2. Complexity: constant time. 3. Requires: The program shall not alter any of the values stored in the character array.

Problem

What is the internal structure of std::wstring? Does it include the length? Is it null terminated? Both?

Original source

Related problems