[string] Are std::string's null terminated?
Sorry if this is off-topic. Just wanted to check .... are std::string's defined to be null terminated? I came across a web page that indicated they are, but you can find lots of misinformation on the web. http://www.codeproject.com/string/cppstringguide2.asp std::string's seem to be null terminated with the Microsoft vc7.1 and vc8 compiler, but I wanted to check if this is generally true according to the C++ standard. I'm relatively new to stl and using std::string .... mostly have used C strings and MFC CStrings (which changed from vc6 to vc7.1?) std::string strA = "ABC"; std::string strB = "DEF"; std::string strC = "GHI"; std::string strD = "JKL"; std::string strE = "MNO"; // Check if std::string's are null terminated const char* pTest = strC.c_str(); for (int i = 0; i < 80; ++i) { printf("I: %2d ch:%3d %c\n", i, pTest[i], pTest[i]); } std::string testStrings[] = { "abc", "def", "ghi", "jkl" }; const char* p = testStrings[1].c_str(); printf("\n********************\n"); for (int i = 0; i < 80; ++i) { printf("I: %2d ch:%3d %c\n", i, p[i], p[i]); } printf("\n********************\n"); TIA,
On 4/8/06, Lynn Allan
Sorry if this is off-topic.
Just wanted to check .... are std::string's defined to be null terminated?
I came across a web page that indicated they are, but you can find lots of misinformation on the web. http://www.codeproject.com/string/cppstringguide2.asp
std::string's seem to be null terminated with the Microsoft vc7.1 and vc8 compiler, but I wanted to check if this is generally true according to the C++ standard.
I'm relatively new to stl and using std::string .... mostly have used C strings and MFC CStrings (which changed from vc6 to vc7.1?)
std::string::c_str() returns a pointer to a null-terminated string, yes.
On 4/8/06, Lynn Allan
wrote: Sorry if this is off-topic.
Just wanted to check .... are std::string's defined to be null terminated?
I came across a web page that indicated they are, but you can find lots of misinformation on the web. http://www.codeproject.com/string/cppstringguide2.asp
std::string's seem to be null terminated with the Microsoft vc7.1 and vc8 compiler, but I wanted to check if this is generally true according to the C++ standard.
I'm relatively new to stl and using std::string .... mostly have used C strings and MFC CStrings (which changed from vc6 to vc7.1?)
From: "Olaf van der Spek"
std::string::c_str() returns a pointer to a null-terminated string, yes.
Thanks, but that isn't quite my question. I'm interested in the actual internal layout of the data in the actual std::string. I realize that myString.c_str() returns a null terminated string, but my understanding is that the result of c_str() can be a separate buffer, not necessarily a pointer to the buffer used by the std::string. Does the internal data structure of the std::string itself have null termination, by definition of the C++ standard? I looked a little harder, and the vc7.1 and vc8 documentation for std::string.c_str() states: Objects of type string belonging to the C++ template class basic_string<char> are not necessarily null terminated. The null character ' \0 ' is used as a special character in a C-string to mark the end of the string but has not special meaning in an object of type string and may be a part of the string just like any other character. There is an automatic conversion from const char* into strings, but the string class does not provide for automatic conversions from C-style strings to objects of type basic_string<char>. But it seems like the vc7.1 implementation of c_str provides the actual address of the internal buffer: std::string strC("HIJKL"); pTest = strC.c_str(); for (int i = 0; i < 80; ++i) { printf("I: %2d Address: %d ch:%3d %c\n", i, &pTest[i], pTest[i], pTest[i]); } printf("\n********************\n"); printf("Sizeof std::stringC: %d\n", sizeof(strC)); printf("&strC: %d\n", &strC); printf("strC.c_str(): %d\n", strC.c_str()); printf("(const char*)strC.c_str(): %d\n", (const char*)strC.c_str()); char* pNonConst = (char*)pTest; pNonConst[3] = 'X'; printf("%s %s %s\n", strC.c_str(), pTest, pNonConst); results in: HIJXL HIJXL HIJXL
On 4/8/06, Lynn Allan
From: "Olaf van der Spek"
std::string::c_str() returns a pointer to a null-terminated string, yes.
Thanks, but that isn't quite my question. I'm interested in the actual internal layout of the data in the actual std::string.
I realize that myString.c_str() returns a null terminated string, but my understanding is that the result of c_str() can be a separate buffer, not necessarily a pointer to the buffer used by the std::string. Does the internal data structure of the std::string itself have null termination, by definition of the C++ standard?
That's an implementation detail and I think that the standard doesn't require it.
On 4/8/06, Lynn Allan
wrote: Thanks, but that isn't quite my question. I'm interested in the actual internal layout of the data in the actual std::string.
The standard doesn't force any internal representation for std::string. It could be a linked list of characters, if the implementation felt like being evil. ( More likely would be a rope of some kind. ) I would expect, however, that most implementations wil basically keep it as a null-terminated std::vector<char>-like internal representation for simplicity since c_str() is unfortunatly still rampant. ~ Scott
I'm interested in the actual internal layout of the data in the actual std::string.
The standard doesn't force any internal representation for std::string. It could be a linked list of characters, if the implementation felt like being evil.
I would expect, however, that most implementations wil basically keep it as a null-terminated std::vector<char>-like internal representation for simplicity since c_str() is unfortunatly still rampant.
Embedded null characters are fully allowed and supported as part of the string object (read the C++ standard or any good C++ library book, such as Josuttis) - so not only can you not assume that the internal representation is null char terminated, you can't even assume there's no embedded null chars elsewhere in the string. I've seen many programmers bitten by making bad assumptions on string objects. I used to internally use a std::string object for a "binary buffer" class until I realized there's no guarantee on internal char array contiguousness (as already noted) - so I started using a std::vector<char>, which does guarantee contiguousness (per the first revision of the C++ standard). Note that the "data()" method on the std::string class gives a pointer to a buffer that is not guaranteed to be null char terminated ... Cliff
At 10:42 2006-04-08, Lynn Allan wrote:
On 4/8/06, Lynn Allan
wrote: Sorry if this is off-topic.
Just wanted to check .... are std::string's defined to be null terminated?
I came across a web page that indicated they are, but you can find lots of misinformation on the web. http://www.codeproject.com/string/cppstringguide2.asp
std::string's seem to be null terminated with the Microsoft vc7.1 and vc8 compiler, but I wanted to check if this is generally true according to the C++ standard.
I'm relatively new to stl and using std::string .... mostly have used C strings and MFC CStrings (which changed from vc6 to vc7.1?)
From: "Olaf van der Spek"
std::string::c_str() returns a pointer to a null-terminated string, yes.
Thanks, but that isn't quite my question. I'm interested in the actual internal layout of the data in the actual std::string.
bad idea all around. there are no guarantees of an internal layout for std::string. I'm curious, though, _why_ are you interested?
[bunch of irrelevant stuff deleted]
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"
bad idea all around. there are no guarantees of an internal layout for std::string. I'm curious, though, _why_ are you interested?
[bunch of irrelevant stuff deleted]
Victor A. Wagner Jr. http://rudbek.com
Agreed ... I was doing some low-level stuff and got to wondering if it would be portable.
Thanks, but that isn't quite my question. I'm interested in the actual internal layout of the data in the actual std::string.
I realize that myString.c_str() returns a null terminated string, but my understanding is that the result of c_str() can be a separate buffer, not necessarily a pointer to the buffer used by the std::string. Does the internal data structure of the std::string itself have null termination, by definition of the C++ standard?
No it doesn't have to have the NULL character at the end, and I believe at least some versions of libstdc++ don't put the NULL on the end unless you call c_str(). John.
participants (7)
-
Alan M. Carroll
-
Cliff Green
-
John Maddock
-
Lynn Allan
-
me22
-
Olaf van der Spek
-
Victor A. Wagner Jr.