In this line:
const char* cstr2 = ss.str().c_str();
ss.str()
will make a copy of the contents of the stringstream. When you call c_str()
on the same line, you'll be referencing legitimate data, but after that line the string will be destroyed, leaving your char*
to point to unowned memory.