I wonder if the void func(const char *str); refer to a valid str if I wrote as follow:
auto str = string("hello").c_str();
func(str);
How is it different from code below?
func(string("hello").c_str())
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From Paragraph 12.2/3 of the C++11 Standard:
This means that the temporary created within the expression that contains the call to
func()will live until function call returns.On the other hand, the lifetime of the temporary in the first code snippet will end up before
func()is invoked, andstrwill be dangling. This will result in Undefined Behavior.