Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is wstring null terminated?

Tags:

c++

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

like image 483
Jonathan Allen Avatar asked Jul 30 '13 06:07

Jonathan Allen


1 Answers

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.

like image 93
Rapptz Avatar answered Nov 03 '22 02:11

Rapptz