Skip to content

Commit e2d2516

Browse files
authored
Merge pull request MicrosoftDocs#2473 from MicrosoftDocs/FromPublicMasterBranch
Confirm merge from FromPublicMasterBranch to master to sync with https://github.com/MicrosoftDocs/cpp-docs (branch master)
2 parents 777efa0 + 9bfcb31 commit e2d2516

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

docs/standard-library/basic-string-class.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,23 +1542,26 @@ The copied characters array2 is: World
15421542

15431543
## <a name="data"></a> basic_string::data
15441544

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.
15461546

15471547
```cpp
1548-
const value_type *data() const;
1548+
const value_type *data() const noexcept;
1549+
value_type *data() noexcept;
15491550
```
15501551

15511552
### Return Value
15521553

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()`.
15541555

15551556
### Remarks
15561557

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))`.
15581559

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.
15601561

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.
15621565

15631566
### Example
15641567

0 commit comments

Comments
 (0)