嗨腐败我工作的一个C++项目,我有一个问题。存储数据造成周围可变
下面是我的代码
tempfingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_TYPE_RSA);
char temp[48];
memset(temp, 0, sizeof(temp));
for (i = 0; i < 16; i++)
{
//fingerprintstream << (unsigned char)tempfingerprint[i] << ":";
if (temp[0] == 0)
{
sprintf(temp, "%02X:", (unsigned char)tempfingerprint[i]);
}
else
{
//sprintf(temp, "%s:%02X", temp, (unsigned char)tempfingerprint[i]);
char characters[3];
memset(characters, 0, sizeof(characters));
//If less than 16, then add the colon (:) to the end otherwise don't bother as we're at the end of the fingerprint
sprintf(characters, "%02X:", (unsigned char)tempfingerprint[i]);
strcat(temp, characters);
}
}
//Remove the end colon as its not needed. 48 Will already be null terminated, so the previous will contain the last colon
temp[47] = 0;
return string(temp);
当我运行我的应用程序,我从Visual Studio收到以下错误
Run-Time-Check Failure #2 - Stack around the variable 'temp' was corrupted.
我已经通过Valgrind的和没有错误运行在Linux上相同的代码显示出来,所以我不确定Windows的问题是什么。
感谢您提供的任何帮助。
*我通过Valgrind在Linux上运行相同的代码,并且没有显示任何错误,所以我不确定Windows的问题是什么。* - 这不是Windows的问题。为什么你使用字符数组,并希望它们足够大以容纳你放置在它们中的数据?看起来你在写'C'而不是'C++'。 – PaulMcKenzie
'字符'看起来太短。 – user4581301
如果您计算终止的nul字符,“%02X:”不会生成超过3个字符。 –