You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/standard-library/basic-string-class.md
+9-6Lines changed: 9 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1542,23 +1542,26 @@ The copied characters array2 is: World
1542
1542
1543
1543
## <aname="data"></a> basic_string::data
1544
1544
1545
-
Converts the contents of a string into an array of characters.
1545
+
Converts the contents of a string into a null-terminated array of characters.
1546
1546
1547
1547
```cpp
1548
-
const value_type *data() const;
1548
+
const value_type *data() constnoexcept;
1549
+
value_type *data() noexcept;
1549
1550
```
1550
1551
1551
1552
### Return Value
1552
1553
1553
-
A pointer to the first element of the array containing the contents of the string, or, for an empty array, a non-null pointer that cannot be dereferenced.
1554
+
A pointer to the first element of the null-terminated array containing the contents of the string. For an empty string, the pointer points to a single null character equal to `value_type()`.
1554
1555
1555
1556
### Remarks
1556
1557
1557
-
Objects of type string belonging to the class template basic_string \<char> are not necessarily null terminated. The return type for`data`is not a valid C-string, because no null character gets appended. The null character ' \0 ' is used as a special character in a C-string to mark the end of the string, but has no special meaning in an object of type string and may be a part of the string object just like any other character.
1558
+
The pointer returned by`data`points at a valid range `[data(), data() + size()]`. Each element in the range corresponds to the current data in the string. That is, for every valid offset *n* in the range, `data() + n == addressof(operator[](n))`.
1558
1559
1559
-
There is an automatic conversion from **const char**<strong>\*</strong> into strings, but the string class does not provide for automatic conversions from C-style strings to objects of type **basic_string \<char>**.
1560
+
If you modify the contents of the string returned by the **const** overload of `data`, the behavior is undefined. You also get undefined behavior if the terminal null character is changed to any other value. The returned pointer may be invalidated if a non-const reference to the string is passed to a standard library function. It can also be invalidated by a call to a non-const member function. Calls to members `at`, `back`, `begin`, `end`, `front`, `rbegin`, `rend`, and `operator[]` don't invalidate the pointer.
1560
1561
1561
-
The returned string should not be modified, because this could invalidate the pointer to the string, or deleted, because the string has a limited lifetime and is owned by the class string.
1562
+
Prior to C++11, `data` didn't guarantee the returned string was null-terminated. Since C++11, `data` and `c_str` both return a null-terminated string, and are effectively the same.
1563
+
1564
+
The non-const overload is new in C++17. To use it, specify the **/std:c++17** or **/std:c++latest** compiler option.
0 commit comments