【发布时间】:2019-12-08 16:50:56
【问题描述】:
char* mstring(char str) {
std::string mystring = "task:";
mystring.append(str);
return (*char)mystring.c_str();//dangerous use of c_str() the value returned by c_str() is invalid after this call
}
【问题讨论】:
-
mystring分配在堆栈上,因此当函数返回时超出范围,并带走了mystring.c_str()。 -
因为它是局部变量,你试图返回函数的局部变量。