我有以下代码,但它会导致异常.如果没有itoa"游戏",我认为没有问题.TextOutA是因为我使用winapi.
char* p1 = new char[2];
itoa(10,p1,10);
TextOutA(hDC,5, currenty,p1,2);
delete[] p1;
Run Code Online (Sandbox Code Playgroud)
你的stringbuffer太短了
itoa在写入空终止字符时超出了缓冲区容量.
char* p1 = new char[3];
itoa(10,p1,10);
TextOutA(hDC,5, currenty,p1,2);
delete[] p1;
Run Code Online (Sandbox Code Playgroud)
我建议你让缓冲区大到足以容纳整数范围.
编辑以防万一,详细说明字符串流建议:
#include <sstream>
//....
{
std::stringstream ss;
ss << 10;
std::string s = ss.str();
TextOutA(hDC, 5, currenty, s.c_str(), s.length());
}
Run Code Online (Sandbox Code Playgroud)