I want to convert a process pid to a const char* but below does not work:
std::ostringstream str_pid;
str_pid << getpid();
const char * cstr_pid = str_pid.str().c_str();
It works most of the time but sometimes it has a false result. Apparently i am doing something wrong.
Any idea?
cstr_pidwill be a dangling pointer, as the temporarystd::stringreturned bystr_pid.str()is destructed after the assignment ofcstr_pid. Create a copy of thestr_pid.str()return value:then use
my_pid.c_str()whenconst char*is required.