I have an old project to maintain which used const char * around. For some reason, I want to keep lots of runtime generated string. So I create a global variable std::set for keeping these string. When a new string is generated, besides to be added into the set, also I would return and sends the newString.c_str() out which will be kept at somewhere else. For example.
std::set<std::string> g_stringDB;
void ArchieveString( AStruct *container, const char *temporaryString )
{
auto it = g_stringDB.emplace( temporaryString );
container->validString = it->first->c_str();
}
I am wonderring when container is used by outside ( I mean anywhere out of this function ). If pointer: validString still safe. Is it possible that the poiner already points to other stuff due to the copy, construct happened within the set? If no, what is an ideal method to implement this requirement?