libstdc++/8636: global ostringstreams lose their data

Michael Pronath
Wed Nov 20 06:59:00 GMT 2002

On Wed, 2002-11-20 at 13:48, 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.

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

Redhat system:   glibc-devel-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

#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 << " is at 0x" << hex << (int)
	 << ", 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

ldd teststream => /usr/lib/ (0x4001a000) => /lib/ (0x400c4000) => /lib/ (0x400e5000) => /lib/ (0x400ed000)
	/lib/ => /lib/ (0x40000000)

Unexpected output:
teststring is 'Hello Hello Hello Hello Hello Hel'.
teststream.str() is 'World, where are you?'. 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.


More information about the Gcc-bugs mailing list