C ++程序错误:malloc():内存损坏

问题描述 投票:0回答:1

C ++新手,并且一直在编写C ++程序,但最终从ctime调用lib函数调用时中断了。

错误显示如下信息:malloc():内存损坏

AFAIK,此错误(内存损坏)应该是由于对越界内存地址进行操作而导致的。并且打印格式代表[YYYY-MM-DD-HH-MM],列在here中,表示长度一定要小于100。

附加信息:-程序编译时带有以下标志:-O3 -g -Wall -Wextra -Werror -std = c ++ 17-编译器:g ++ 7.4.0-系统:WSL Ubuntu-18

#include <ctime> 
#include <stdio>

int main() {
    char buff[100];
    get_timestamp(buff, 100);
    cout << string(buff);
    return 0;
}

void get_timestamp(char *buffer, int len)
{
    time_t raw_time;
    struct tm *time_info;
    time(&raw_time);
    time_info = localtime(&raw_time);                // the line of code that breaks
    strftime(buffer, len, "%F-%H-%M", time_info); 
    return;
}

感谢您的任何建议或想法。

C ++新手,并且一直在编写C ++程序,但最终从ctime调用lib函数调用时中断了。该错误显示如下信息:malloc():内存损坏AFAIK,此...

c++ memory-management ctime memory-corruption
1个回答
0
投票

David提到的“明显的修正”是:

© www.soinside.com 2019 - 2024. All rights reserved.