By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,134 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,134 IT Pros & Developers. It's quick & easy.

\0 in std::string

P: n/a
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
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
X-Replace-Address: yes
Mar 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Stefan Naewe wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 16 '07 #2

P: n/a
On 3/16/2007 2:10 PM, Victor Bazarov wrote:
Stefan Naewe wrote:
>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.
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
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
X-Replace-Address: yes
Mar 16 '07 #3

P: n/a
Stefan Naewe wrote:
On 3/16/2007 2:10 PM, Victor Bazarov wrote:
>Stefan Naewe wrote:
>>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.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 16 '07 #4

P: n/a
On 3/16/2007 3:03 PM, Victor Bazarov wrote:
Stefan Naewe wrote:
>On 3/16/2007 2:10 PM, Victor Bazarov wrote:
>>Stefan Naewe wrote:
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.
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?
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.
>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?
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.
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
X-Replace-Address: yes
Mar 19 '07 #5

P: n/a
"Stefan Naewe" <no*****@please.netwrote in message
news:d0************@news01.atlas.de...
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
Mar 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.