This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/25719] ostringstream::str().c_str() returns pointer to unallocated memory
- From: "pinskia at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 9 Jan 2006 16:24:56 -0000
- Subject: [Bug libstdc++/25719] ostringstream::str().c_str() returns pointer to unallocated memory
- References: <bug-25719-7638@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #4 from pinskia at gcc dot gnu dot org 2006-01-09 16:24 -------
Here is the testcase from the tar ball for us lazy people:
#include <iostream>
#include <sstream>
using namespace std;
int printit(const char * const str) {
return *str;
}
int main()
{
ostringstream ostr;
ostr << "test test test" << ends;
const char * const str = ostr.str().c_str();
printit(str);
return 0;
}
-----
ostr.str() returns a temporary variable which dies right after the end of the
statement which means that the return value of c_str() becomes invalid after
the statement finishes.
This is exactly what Paolo said in comment #2.
--
pinskia at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25719