我正在检查源代码并看到这个for
循环:
for (int i=0; i < result1.length(); i++) {
unsigned char *buff = ascii_to_utf8((unsigned char)result1.at(i));
result.append((char*)buff);
free (buff);
}
已改为:
for (int i=0; i < result1.length(); i++)
result.append((char*)ascii_to_utf8((unsigned char)result1.at(i)));
我能看到的唯一区别是在第二个代码中没有中间变量。
我的问题是,我看不到的两个代码片段之间有区别吗?free
seconde 代码中没有任何内容吗?
------------ 编辑 ------------ 这是 ascii_to_utf8 的源代码:
unsigned char* InvoiceXML::ascii_to_utf8(unsigned char c)
{
unsigned char *out;
if(c < 128)
{
out = (unsigned char *)calloc(2, sizeof(char));
out[0] = c;
out[1] = '\0';
}
else
{
out = (unsigned char *)calloc(3, sizeof(char));
out[0] = (c >> 6) | 0xC0;
out[1] = (c & 0x3F) | 0x80;
out[2] = '\0';
}
return out;
}