This is the mail archive of the
gcc-prs@gcc.gnu.org
mailing list for the GCC project.
libstdc++/5737: ostringstream::str() appends \0 character
- From: wolfgang dot bangerth at iwr dot uni-heidelberg dot de
- To: gcc-gnats at gcc dot gnu dot org
- Date: 20 Feb 2002 17:59:59 -0000
- Subject: libstdc++/5737: ostringstream::str() appends \0 character
- Reply-to: wolfgang dot bangerth at iwr dot uni-heidelberg dot de
>Number: 5737
>Category: libstdc++
>Synopsis: ostringstream::str() appends \0 character
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Wed Feb 20 10:06:04 PST 2002
>Closed-Date:
>Last-Modified:
>Originator: Wolfgang Bangerth
>Release: unknown-1.0
>Organization:
>Environment:
Linux 2.4.14, gcc version 3.1 20020215 (experimental)
I believe that gcc3.0.x has the same problem
>Description:
When extracting a string from an ostringstream object using
the str() function, the std::string also has stored a final
\0 character. This is not usually visible in output since
such characters are not printed, but of course is very much
confusing when your programs tells you that the string "x"
has characters other than 'x'!
I think that originally the \0 slips in when you "close" the
ostringstream object by std::ends, but it is not stripped
when the internal string is copied to the result of str().
Regards
Wolfgang
>How-To-Repeat:
Use this program to see what happens:
-------------------------------------
#include <sstream>
#include <iostream>
using namespace std;
int main () {
// create a string using an ostringstream
ostringstream s; s << "x" << ends;
// output its content and size
cout << '\"' << s.str() << '\"' << endl;
cout << s.str().size() << endl;
// output each of its characters
for (unsigned int i=0; i<s.str().size(); ++i)
cout << "Character " << i << ": \'" << s.str()[i]
<< "\' (value=" << static_cast<int>(s.str()[i]) << ")" << endl;
};
-----------------------------------------------
This is its output:
"x"
2
Character 0: 'x' (value=120)
Character 1: '' (value=0)
Clearly the second character should not be there.
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted: