\0 in std::string

S

Stefan Naewe

A simple test:

<---------------------------->
#include <string>
#include <iostream>

int main()
{
std::string h("Hello ");
*h.rbegin() = '\0';
std::string w=h+std::string(" World");

std::cout << w << std::endl;
return 0;
}
<---------------------------->

Output with different compilers:

MSVC6 :
"Hello 12"

gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21):
"Hello World 12"

I know MSVC6 is severely borken but is it correct anyway ?


Thanks
Stefan
 
V

Victor Bazarov

Stefan said:
A simple test:

<---------------------------->
#include <string>
#include <iostream>

int main()
{
std::string h("Hello ");
*h.rbegin() = '\0';
std::string w=h+std::string(" World");

std::cout << w << std::endl;
return 0;
}
<---------------------------->

Output with different compilers:

MSVC6 :
"Hello 12"

gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21):
"Hello World 12"

I know MSVC6 is severely borken but is it correct anyway ?

The resulting string (before output) should be (without the quotes
of course):

"Hello\0 World"

Now, what you see when you output it has been converted into the
output medium representation. I just tested it with VC++ v8, and
got two spaces between the words, which suggests that the null
character is replaced with a space in *my* output.

In order to verify the contents of the string, you need to print it
out char by char.

V
 
S

Stefan Naewe

The resulting string (before output) should be (without the quotes
of course):

"Hello\0 World"

Now, what you see when you output it has been converted into the
output medium representation. I just tested it with VC++ v8, and
got two spaces between the words, which suggests that the null
character is replaced with a space in *my* output.

In order to verify the contents of the string, you need to print it
out char by char.

Which I have done already...and the gcc version indeed outputs the '\0'.

The problem is that MSVC6's operator<<() for std::basic_string<>
more or less does

stream << str.c_str();

which in turn uses strlen(s) the get the length of the sequence to be output.
So, another stupidity in our beloved compiler...

Thanks
Stefan
 
V

Victor Bazarov

Stefan said:
Which I have done already...and the gcc version indeed outputs the
'\0'.

The problem is that MSVC6's operator<<() for std::basic_string<>
more or less does

stream << str.c_str();

How do you know that? What if it's the 'cout's problem and not the
operator's? The operator stuffs the characters into the buffer. What
the buffer does with those characters is implementation-defined, AFAIK.
which in turn uses strlen(s) the get the length of the sequence to be
output. So, another stupidity in our beloved compiler...

Uh... Wait a minute. But by extension, gcc is also wrong. What's that
"12" at the end of each of them? Is that how your system displays the
'\n'? Who's to say how your system reacts to the \0 in the output
stream?

V
 
S

Stefan Naewe

How do you know that?

I looked at the code (It's a template, you know...)
What if it's the 'cout's problem and not the
operator's? The operator stuffs the characters into the buffer. What
the buffer does with those characters is implementation-defined, AFAIK.


Uh... Wait a minute. But by extension, gcc is also wrong. What's that
"12" at the end of each of them? Is that how your system displays the
'\n'? Who's to say how your system reacts to the \0 in the output
stream?

My bad...
I did what I never wanted to do.
It's a copy-and-waste error. The actual code is this:

std::cout << w << ' ' << w.length() << std::endl;


So the '12' is the length of the string.

I guess, MSVC is simply wrong and GCC is right.


S.
 
P

P.J. Plauger

I guess, MSVC is simply wrong and GCC is right.

Nothing simple about it. You're comparing an eleven year old
version of VC++ against today's gcc. Yes, V6 had a bug in
the string inserter, which has been long since fixed. The
interesting thing is that many people still find V6 useful,
after all these years, despite the fact that it predates the
C++ standard and despite the fact that there have been three
subsequent updates from Microsoft.

Now if you'd care to compare the V6 library against what
passed for the gcc C++ library circa 1996...

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top