我有以下结构
typedef struct a_t
{
vector <int> a;
int p;
}a;
typedef struct b_t
{
a x;
int y;
}b;
struct a 是一个包含向量的结构,struct b 包含 struct a 我想将 struct b 写入/读取到二进制文件中。以下代码不起作用
int main()
{
b m;
m.x.a.push_back(1);
m.x.a.push_back(2);
m.x.a.push_back(3);
m.x.p = 5;
m.y = 7;
cout << sizeof(m.y) << endl;
cout << sizeof(m.x) << endl;
cout << sizeof(m) << endl;
ofstream outfile("model",ios::out|ios::binary);
outfile.write((char*)&m,sizeof(m));
outfile.close();
b p;
ifstream iffile("model", ios::in|ios::binary);
iffile.read((char*)&p,sizeof(a));
iffile.close();
cout << p.y << endl;;
cout << p.x.p << endl;
cout << p.x.a[0] << endl;
cout << p.x.a[1] << endl;
cout << p.x.a[2] << endl;
return 0;
}
错误消息是“ * glibc 检测到双重释放或损坏(顶部):0x0000000000504010 * * 中止(核心转储)”旁边,它不会将结构写入文件。