9

We all know there is a null character automatically attached to the end of a C-string...How about C++ string object? Is there also a null character at the end of it?

Thank you so much!

user1050165
  • 141
  • 1
  • 7

3 Answers3

9

In C++03, a std::string was not required to be stored in a NUL-terminated buffer, but if you called c_str() it would return a pointer to a NUL-terminated buffer. That buffer could legally be created and/or terminated inside the c_str() call.

In C++11, all std::string instances are terminated, so data() also addresses a NUL-terminated buffer, and even s[s.size()] has a well-defined meaning returning a reference to the terminating NUL.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • So it boils down to what language you want to interface with? If you need to interface to a C function, use c_str() to get a pointer to a null-terminated character array. If you need to interface with another C++ function it probably expects std::string, which is not guaranteed to be null-terminated (if you need to cater for C++03 as well). – keplerian May 13 '15 at 07:45
  • 1
    @Keplerian: you would indeed use `c_str()` when calling a C function expecting an ASCIIZ buffer via `const char*`, and might use `&s[0], s.size()` to pass a (non-`const) `char*` and `size_t` to a C function planning to modify the buffer content - but before C++11 it's not guaranteed NUL-terminated and you must check `!s.empty()` first to avoid `&s[0]` undefined behaviour. For other C++ functions expecting `std::string`, you shouldn't have to promise them anything about the NUL termination, as they'll presumably use `.size()`, `.end()` etc.. – Tony Delroy May 13 '15 at 08:47
  • See also [my answer here](http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char/4152881#4152881). – Tony Delroy May 13 '15 at 08:48
4

std::string is very much a std::vector: it has a length attribute and is not zero terminated in C++03; in C++11 'std::string' does seems to be terminated, but I find it easier to think of 'std::string' as a 'std::vector' of characters and not just a terminated buffer.

Arno Duvenhage
  • 1,910
  • 17
  • 36
  • 3
    Additionally, `c_str()` can be used to get the C-type null terminated string. – Naveen Jul 22 '13 at 05:31
  • 5
    It is not as simple as "no". There doesn't have to be a null termination between `[begin(), end())`, but both `c_str()` and `data()` return a pointer to a null terminated array. – juanchopanza Jul 22 '13 at 05:41
  • @juanchopanza That's in C++11 only. – Tony Delroy Jul 22 '13 at 05:44
  • @TonyD Correct. The situation is quite complicated in C++03. But I didn't mention C++03, so my comment refers to C++11 (it being 2013 and all :-) ) – juanchopanza Jul 22 '13 at 05:46
  • @juanchopanza: yes, 2013 and all and no C++11 compliant compilers and relatively few corporate projects using the latest near-compliant compilers... ;-). Sadly, I think it'll be useful to document differences for another year or two.... – Tony Delroy Jul 22 '13 at 06:41
-1

std::string::data

Returns a pointer to an array that contains the same sequence of characters as the characters that make up the value of the string object.

Accessing the value at data()+size() produces undefined behavior: There are no guarantees that a null character terminates the character sequence pointed by the value returned by this function. See string::c_str for a function that provides such guarantee.

A program shall not alter any of the characters in this sequence.

Anand Rathi
  • 790
  • 4
  • 11
  • 4
    In C++11, `data()` and `c_str()` are actually the same -- both will be null terminated. – Cory Nelson Jul 22 '13 at 05:43
  • 1
    +1 @CoryNelson Thanks , Cx11 std::string::data : Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object. This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character ('\0') at the end. The pointer returned points to the internal array currently used by the string object to store the characters that conform its value. Both string::data and string::c_str are synonyms and return the same value. – Anand Rathi Jul 22 '13 at 05:51