473,587 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

\0 in std::string

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_naew e_AT_atlas_DOT_ de
X-Replace-Address: yes
Mar 16 '07 #1
5 2357
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
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_stri ng<>
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_naew e_AT_atlas_DOT_ de
X-Replace-Address: yes
Mar 16 '07 #3
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_stri ng<>
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
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_stri ng<>
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_naew e_AT_atlas_DOT_ de
X-Replace-Address: yes
Mar 19 '07 #5
"Stefan Naewe" <no*****@please .netwrote in message
news:d0******** ****@news01.atl as.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
8160
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
11
3632
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
22
13229
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
19
6133
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
8
9177
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be a namespace and another typedef to basic_string: namespace my_string {
6
11492
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for example string str1; as a class member and then add text to it. or do I have to specify it's length when defining? 2. How to convert from std::string to LPCSTR
2
5488
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
15830
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
2888
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
11
4246
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr. Yick. All I could come up with is: #include <string> int main() { std::string text;
0
7924
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.