我对这段代码有点问题:
string StringServices::ToStringf(float value)
{
char buffer[10];
sprintf (buffer, "%f", value);
return (string) buffer; // signal SIGABRT
}
Run Code Online (Sandbox Code Playgroud)
它以前一直在工作,并继续为其他调用工作,但我现在得到一个SIGABRT返回,当函数通过-211.0
缓冲区加载很好,我真的不确定为什么这不起作用.能理解std :: string和c字符串的人能帮到我吗?
您可能因为没有使用而溢出缓冲区snprintf.你有这个标记的C++所以这样做:
std::string buffer = boost::lexical_cast<std::string>(value);
或者没有提升使用字符串流:
std::ostringstream os;
os << value;
// os.str() has the string representation now.
Run Code Online (Sandbox Code Playgroud)