This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: libstdc++/8636: global ostringstreams lose their data
- From: Michael Pronath <michael dot pronath at gmx dot de>
- To: Paolo Carlini <pcarlini at unitus dot it>
- Cc: gcc-gnats at gcc dot gnu dot org, gcc-prs at gcc dot gnu dot org, gcc-bugs at gcc dot gnu dot org, nobody at gcc dot gnu dot org
- Date: 21 Nov 2002 13:11:49 +0100
- Subject: Re: libstdc++/8636: global ostringstreams lose their data
- References: <3DDCBB47.1040807@unitus.it>
Hello again,
I think I found the reason for this, it's not a bug.
If 'oss' is an ostringstream, then calling
x = oss.str().c_str();
is a bad idea, because oss.str() returns a copy of the contents of oss
by value, i.e. a temporary value that is free'd right after this
statement. Therefore x is invalid, pointing to a free'd memory region.
--> oss.str().c_str() will always return a pointer to a temporary
value.
For example, strcpy(dest, oss.str().c_str()) would work, but
not x=oss.str().c_str(); strcpy(dest,x);
I believe this is ok with the standard.
Sorry, my fault.
Regards,
Michael