From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20813 invoked by alias); 21 Nov 2002 10:56:02 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 20794 invoked by uid 71); 21 Nov 2002 10:56:02 -0000 Date: Sat, 30 Nov 2002 20:01:00 -0000 Message-ID: <20021121105602.20793.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: Paolo Carlini Subject: Re: libstdc++/8636: global ostringstreams lose their data Reply-To: Paolo Carlini X-SW-Source: 2002-11/txt/msg01205.txt.bz2 List-Id: The following reply was made to PR libstdc++/8636; it has been noted by GNATS. From: Paolo Carlini To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org Cc: michael.pronath@gmx.de Subject: Re: libstdc++/8636: global ostringstreams lose their data Date: Thu, 21 Nov 2002 11:53:59 +0100 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 #include #include 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 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8636