[string] Are std::string's null terminated?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[string] Are std::string's null terminated?

l_d_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.
 
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,

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

Olaf van der Spek-2
On 4/8/06, Lynn Allan <[hidden email]> 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?)

std::string::c_str() returns a pointer to a null-terminated string, yes.
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

Alan M. Carroll
In reply to this post by l_d_allan
std::string is *not* defined to be null terminated. There is nothing that forbids an implementation from doing so, but it is not required. As noted in a previous response, you must call std::string::c_str() if you need to guarantee null termination. However, this method can cause a re-allocation of the string object (which can be an issue in multi-threaded situations).

It turns out in practice to rarely be important. For instance, if you use C++ IO operators instead of printf(), null termination is irrelevant. If you need to iterate over the characters in a string, you can use std::string::size() or std::string::begin () / end(). All operators on std::strings work regardless of termination. It is only when interacting with C interfaces that it matters and the simple solution is to not do that :-).

At 10:49 AM 4/8/2006, Lynn Allan wrote:
Sorry if this is off-topic.
 
Just wanted to check .... are std::string's defined to be null terminated?

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

l_d_allan
In reply to this post by Olaf van der Spek-2
> On 4/8/06, Lynn Allan <[hidden email]> 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" <[hidden email]>
> 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


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

Olaf van der Spek-2
On 4/8/06, Lynn Allan <[hidden email]> wrote:

> From: "Olaf van der Spek" <[hidden email]>
> > 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.
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

me22
> On 4/8/06, Lynn Allan <[hidden email]> 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
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

Cliff Green-3
>> 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

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

Victor A. Wagner Jr.
In reply to this post by l_d_allan
At 10:42 2006-04-08, Lynn Allan wrote:

> > On 4/8/06, Lynn Allan <[hidden email]> 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" <[hidden email]>
> > 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"

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

John Maddock
In reply to this post by l_d_allan
> 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.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] Are std::string's null terminated?

l_d_allan
In reply to this post by Victor A. Wagner Jr.

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


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users