为什么我会出现“堆损坏”?

发布于 2024-08-29 01:18:11 字数 884 浏览 8 评论 0原文

请不要因为这件事而把我钉在十字架上。我认为使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朦胧时间 2024-09-05 01:18:11

您需要再分配一个字节,因为 string.length 返回其长度而没有终止 NUL,为此您还需要 char*< 中的空间/代码>。

即假设 Message.length() 返回 10。您分配了 21 个字节。将11个字节复制到缓冲区中,然后复制消息,这需要10个字节+1个NUL。总计:22 个字节,而您只分配了 21 个字节。

You need to allocate one more byte, since .length of a string returns its length without the terminating NUL, for which you also need space in the char*.

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.

享受孤独 2024-09-05 01:18:11

char* MessageBuffer = new char[Message.length()+11];

应该是

char* MessageBuffer = new char[Message.length()+12];

因为您要向缓冲区添加 11 个附加字符:

2 for hr
2 for ::
2 for min
2 for ::
2 for sec
1 for " "

您需要一个附加字符作为终止 null 字符。

This

char* MessageBuffer = new char[Message.length()+11];

should be

char* MessageBuffer = new char[Message.length()+12];

Because you are adding 11 additional char to the buffer:

2 for hr
2 for ::
2 for min
2 for ::
2 for sec
1 for " "

you need one additional to for the terminating null char.

吻安 2024-09-05 01:18:11

正如其他人指出的那样,MessageBuffer 的大小需要增加一。

但是,您可以将时间信息直接流式传输到 _file,而不是以这种方式处理原始字符缓冲区,而无需先将其放入中间字符串中。如果您出于某种原因希望将其放在中间字符串中,我建议您使用 ostringstream 类。

void writelogmessage(std::string Message)
{
    time_t rawtime;
    struct tm* timeinfo = 0;

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    std::ostringstream stream;
    stream<<
        timeinfo->tm_hour<<"::"<<
        timeinfo->tm_min<<"::"<<
        timeinfo->tm_sec<<" "<<
        Message;

    _file<<stream.str();
}

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 the ostringstream class.

void writelogmessage(std::string Message)
{
    time_t rawtime;
    struct tm* timeinfo = 0;

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    std::ostringstream stream;
    stream<<
        timeinfo->tm_hour<<"::"<<
        timeinfo->tm_min<<"::"<<
        timeinfo->tm_sec<<" "<<
        Message;

    _file<<stream.str();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文