Possible Duplicate:
Lifetime of temporaries
I'm assuming std::string::c_str()
returns a pointer to some internal (to std::string
) data. Pretend this is true if it's not. In the following, name
is set is to point to this internal data. When is the std::string
returned by GetString()
destroyed, causing name to be invalid?
#include <string>
#include <stdio.h>
using namespace std;
string GetString() {return string("TheString");}
int main(int argc, const char* argv[])
{
const char* name = GetString().c_str();
printf("name = %s\n",name);
}
// Should print "name = TheString"