In the following code, temporary string object is created and destroyed after returning its internal buffer. Therefore p
points to an invalid memory location.
int main()
{
const char* p = std::string("testing").c_str();
//p points to an invalid memory location
}
In the following code, temporary string object is created. When is the tempory string object get deleted? After or before executing func()
? Can I safely use the p pointer inside the func()
method?
void func(const char* p)
{
//p is valid or invalid ????
}
int main()
{
func(std::string("testing").c_str());
}