glibc检测到双重免费或腐败

nik*_*kel 1 c++ crash glibc

下面的代码有什么问题.对于某些输入和某些特殊输入的崩溃,它运行完全正常吗?

#include<iostream>
#include<string>
#include<fstream>

using namespace std;


struct event { 

string date,time,content;
bool is_high_priority;

};


int main() {

event one,two;
one.is_high_priority=false;
char tmp;

ofstream out_file("events" , ios::binary );


    cout<<"\nEnter Date(dd.mm) ";
    cin>>one.date;
    cout<<"\nEnter Time(hh:mm:ss) ";
    cin>>one.time;
    cout<<"\nenter content";
    cin>>one.content;

    if(tmp == 't') 
        one.is_high_priority = true;
    else
        one.is_high_priority = false;


    out_file.write((char*) &one, sizeof(one) );

    out_file.close();


    ifstream in_file("events" , ios::binary );
    in_file.read((char*)&two,sizeof(two));

    cout<<two.date<<" "<<two.time<<" "<<two.content<<" "<<two.is_high_priority;

    in_file.close();

}
Run Code Online (Sandbox Code Playgroud)

它为这些输入而崩溃:输入日期(dd.mm)ankmjjdn md

输入时间(hh:mm:ss)输入contentsnjs sjnsn

sth*_*sth 6

您不能只将std::string对象的字节保存到文件中,然后再次加载它们.该std::string包含指向动态分配的内存,和您保存/载入只会复制指针本身,而不是指向的数据.