为什么我会出现“堆损坏”?
请不要因为这件事而把我钉在十字架上。我认为使用 char* 可能会更好,因为我打算构建的字符串的大小已知。我还知道,如果 timeinfo->tm_hour 返回 2 位数以外的数字,事情就会严重错误。也就是说,当这个函数返回时,VIsual Studio 就开始对我说 HEAP CORRUPTION。出了什么问题? (另外,我应该使用字符串生成器吗?)
void cLogger::_writelogmessage(std::string Message)
{
time_t rawtime;
struct tm* timeinfo = 0;
time(&rawtime);
timeinfo = localtime(&rawtime);
char* MessageBuffer = new char[Message.length()+11];
char* msgptr = MessageBuffer;
_itoa(timeinfo->tm_hour, msgptr, 10);
msgptr+=2;
strcpy(msgptr, "::");
msgptr+=2;
_itoa(timeinfo->tm_min, msgptr, 10);
msgptr+=2;
strcpy(msgptr, "::");
msgptr+=2;
_itoa(timeinfo->tm_sec, msgptr, 10);
msgptr+=2;
strcpy(msgptr, " ");
msgptr+=1;
strcpy(msgptr, Message.c_str());
_file << MessageBuffer;
delete[] MessageBuffer;
}
Please don't crucify me for this one. I decided it might be good to use a char* because the string I intended to build was of a known size. I am also aware that if timeinfo->tm_hour returns something other than 2 digits, things are going to go badly wrong. That said, when this function returns VIsual Studio goes ape at me about HEAP CORRUPTION. What's going wrong? (Also, should I just use a stringbuilder?)
void cLogger::_writelogmessage(std::string Message)
{
time_t rawtime;
struct tm* timeinfo = 0;
time(&rawtime);
timeinfo = localtime(&rawtime);
char* MessageBuffer = new char[Message.length()+11];
char* msgptr = MessageBuffer;
_itoa(timeinfo->tm_hour, msgptr, 10);
msgptr+=2;
strcpy(msgptr, "::");
msgptr+=2;
_itoa(timeinfo->tm_min, msgptr, 10);
msgptr+=2;
strcpy(msgptr, "::");
msgptr+=2;
_itoa(timeinfo->tm_sec, msgptr, 10);
msgptr+=2;
strcpy(msgptr, " ");
msgptr+=1;
strcpy(msgptr, Message.c_str());
_file << MessageBuffer;
delete[] MessageBuffer;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要再分配一个字节,因为
string
的.length
返回其长度而没有终止 NUL,为此您还需要char*< 中的空间/代码>。
即假设
Message.length()
返回 10。您分配了 21 个字节。将11个字节复制到缓冲区中,然后复制消息,这需要10个字节+1个NUL。总计:22 个字节,而您只分配了 21 个字节。You need to allocate one more byte, since
.length
of astring
returns its length without the terminating NUL, for which you also need space in thechar*
.I.e. suppose
Message.length()
returns 10. You allocate 21 bytes. Copy 11 bytes into the buffer, then copy the message, which needs 10 bytes + one for NUL. Total: 22 bytes, and you only have 21 allocated.这
应该是
因为您要向缓冲区添加
11
个附加字符:您需要一个附加字符作为终止
null
字符。This
should be
Because you are adding
11
additional char to the buffer:you need one additional to for the terminating
null
char.正如其他人指出的那样,
MessageBuffer
的大小需要增加一。但是,您可以将时间信息直接流式传输到
_file
,而不是以这种方式处理原始字符缓冲区,而无需先将其放入中间字符串中。如果您出于某种原因希望将其放在中间字符串中,我建议您使用 ostringstream 类。As others have pointed out, the size of
MessageBuffer
needs to be increased by one.However, rather than dealing with the raw char buffer in that way, you could just stream the time information directly to
_file
without putting it into an intermediate string first. If you want it in an intermediate string for some reason, I would suggest that you make use of theostringstream
class.