This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: libstdc++/8636: global ostringstreams lose their data


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


http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8636


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]