Linked Questions
30 questions linked to/from Is std::string always null-terminated in C++11?
138 votes
6 answers
130k views
Does std::string have a null terminator?
Will the below string contain the null terminator '\0'? std::string temp = "hello whats up";
73 votes
4 answers
10k views
Is string::c_str() no longer null terminated in C++11? [duplicate]
In C++11 basic_string::c_str is defined to be exactly the same as basic_string::data, which is in turn defined to be exactly the same as *(begin() + n) and *(&*begin() + n) (when 0 <= n < ...
9 votes
3 answers
3k views
Is there a null character at end of a std::string? [duplicate]
We all know there is a null character automatically attached to the end of a C-string. What about a std::string object? Is there also a null character at the end of it?
1 vote
2 answers
2k views
Does C++ null-terminate a std::string? [duplicate]
It is possible to call .c_str() for a C++ const string. In order to efficiently implement this, it means that it must internally store an extra null character. (Otherwise, copying or modifying data ...
5 votes
3 answers
648 views
Is C++11's std::string's underlying representation guaranteed to have the terminating null character in place? [duplicate]
Some excerpts from the standard first: Spec for string::operator[](): const_reference operator[](size_type pos) const; reference operator[](size_type pos); Requires: pos <= size(). Returns:...
0 votes
0 answers
67 views
Does std::string append a null terminator upon initialization of a string literal? [duplicate]
#include <iostream> #include <string> #include <cstring> int main() { using namespace std; string cppString = "string1"; //size of C++ String cout << sizeof(...
38 votes
6 answers
6k views
What are the problems of a zero-terminated string that length-prefixed strings overcome?
What are the problems of a zero-terminated string that length-prefixed strings overcome? I was reading the book Write Great Code vol. 1 and I had that question in mind.
53 votes
4 answers
29k views
Can a std::string contain embedded nulls?
For regular C strings, a null character '\0' signifies the end of data. What about std::string, can I have a string with embedded null characters?
26 votes
3 answers
2k views
Difference in c_str function specification between C++03 and C++11
In the C++ reference of c_str() in std::string the following appears: Return value Pointer to the underlying character storage. data()[i] == operator[](i) for every i in [0, size()) (until C++11)...
10 votes
4 answers
29k views
Use of null character in strings (C++)
I am brushing up on my C++ and stumbled across a curious behavior in regards to strings, character arrays, and the null character ('\0'). The following code: #include <iostream> using namespace ...
10 votes
3 answers
6k views
Is wstring null terminated?
What is the internal structure of std::wstring? Does it include the length? Is it null terminated? Both?
3 votes
7 answers
3k views
std::string getting (char *) instead of (const char *)
std::string.c_str() returns a (const char *) value. I Googled and found that I can do the following: std::string myString = "Hello World"; char *buf = &myString[0]; How is this possible? &...
5 votes
1 answer
10k views
Converting from std::string to char * in C++
I'm using VS2012/C++, I need to convert a std::string to char * and I can't find any material online giving any guidance on how to go about doing it. Any code examples and advice will be greatly ...
2 votes
6 answers
6k views
Convert @ sign from byte in GSM 7-bit encoding to Java text
I have given a byte array [97, 98, 0, 99, 100] which is GSM 7-Bit encoded. This should be converted into ab@cd. When I tried to append this given array into a StringBuilder, I was not able to convert ...
7 votes
2 answers
6k views
Is returned string null-terminated from getline()?
One thing I'm not pretty sure after googling for a while, is the returned string of getline(). Hope to get it confirmed here. std::getline This global version returns a std::string so it's not ...