libstdc++/8636: global ostringstreams lose their data

Michael Pronath michael.pronath@gmx.de
Wed Nov 20 06:59:00 GMT 2002


On Wed, 2002-11-20 at 13:48, paolo@gcc.gnu.org wrote:
> Synopsis: global ostringstreams lose their data
> 
> State-Changed-From-To: open->feedback
> State-Changed-By: paolo
> State-Changed-When: Wed Nov 20 04:48:35 2002
> State-Changed-Why:
>     Cannot reproduce on x86-linux (glibc2.3.1) with gcc-3.2.1
>     and current 3.3 exp. Could possibly install gcc-2.3.1 and
>     see if the problem it's still present for you?
>     Thanks, Paolo.
> 
> http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8636
> 

Yes, the problem is present with g++-3.0, g++-3.2 and g++-3.2.1
all with glibc2.3.1

Debian system:   libc6_2.3.1-3
                 g++-3.2_3.2.1-0pre2
                 g++-3.0_3.0.4-7
                 libstdc++5-dev_3.2.1-0pre2

Redhat system:   glibc-devel-2.1.3-22
		 glibc-2.1.3-22
                 g++-3.2   self-made

The original topic was wrong, I found an even simpler example, it's got
nothing to do with global/local variables; it's probably in the
allocator:

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

using namespace std;
   
int main()
{
    const char *x;
    ostringstream teststream;
    string teststring="";
    teststream << "World, where are you?";
    x = teststream.str().c_str();
    teststring = "Hello ";
    teststring += x;
    cout << "teststring is '" << teststring << "'." << endl;
    cout << "teststream.str() is '" << teststream.str() << "'." << endl;
    cout << "teststring.data() is at 0x" << hex << (int) teststring.data()
	 << ", x is at 0x" << hex << (int) x << endl;
    cout << "teststream.str().c_str() is at 0x" << hex
	 << (int) teststream.str().c_str() << endl;
}
------------------------------------------------------------------------


g++-3.2 -o teststream teststream.cc

ldd teststream
	libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x4001a000)
	libm.so.6 => /lib/libm.so.6 (0x400c4000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x400e5000)
	libc.so.6 => /lib/libc.so.6 (0x400ed000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

Unexpected output:
teststring is 'Hello Hello Hello Hello Hello Hel'.
teststream.str() is 'World, where are you?'.
teststring.data() is at 0x804a99c, x ist at 0x804a99c
teststream.str().c_str() is at 0x804a94c


I thinks it's that the operator+= of string is resizing the string
because it needs new space to append.  From the allocator, it receives
the address of "x" as if this space was already freed by the
ostringstream (don't know why).  Then the string appends its data to
itself, which leads to the repeating Hello.


Regards,
Michael




More information about the Gcc-bugs mailing list