Possible duplicate:
Life time in time
I assume that std::string::c_str()
returns a pointer to some internal (before std::string
) data. Pretend it's true, if not. Below is name
to indicate this internal data. When is the std::string
returned by GetString()
destroyed, causing the wrong name?
#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"
source share