嗨我正在研究一个模拟程序,它试图在请求时将程序的状态(变量和对象)保存到二进制文件中,以便在需要时可以恢复模拟 .
就像一个注释:我知道这在不同的CPU架构中是不兼容的,这绝对没问题!
在编写一个具有虚拟方法的对象然后尝试将其读回来之前,一切似乎都工作正常 .
以下代码说明了此问题:
header.hpp
using namespace std;
class parent
{
public:
int mValue;
virtual string getName() =0;
virtual size_t getSize() =0;
parent(int value) : mValue(value)
{
}
};
class bob : public parent
{
public:
bob(int value) : parent(value)
{
}
string getName();
size_t getSize() { return sizeof(bob); }
};
string bob::getName()
{
string name("bob");
return name;
}
class sarah : public parent
{
public:
sarah(int value) : parent(value)
{
}
string getName();
size_t getSize() { return sizeof(sarah); }
};
string sarah::getName()
{
string name("sarah");
return name;
}
write.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "header.hpp"
int main()
{
sarah girl(1);
bob boy(2);
parent* child1 = &girl;
parent* child2 = &boy;
cout << "Created child called " << child1->getName() << endl;
cout << "Created child called " << child2->getName() << endl;
//save sarah and bob to a binary file
ofstream file("temp.bin", ios::binary | ios::trunc);
if(!file.is_open())
return 1;
//format <size><data><size><data>....
size_t tempSize=0;
//write child1
tempSize = child1->getSize();
file.write( (char*) &tempSize,sizeof(size_t));
file.write( (char*) child1,tempSize);
tempSize = child2->getSize();
file.write( (char*) &tempSize,sizeof(size_t));
file.write( (char*) child2,tempSize);
file.close();
return 0;
}
read.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "header.hpp"
int main()
{
//read sarah and bob from a binary file
ifstream file("temp.bin", ios::binary);
//format <size><data><size><data>....
size_t tempSize=0;
//get size of child1
file.read( (char*) &tempSize, sizeof(size_t));
//allocate memory for child1
parent* child1= (parent*) malloc(tempSize);
//read child 1 back
file.read( (char*) child1,tempSize);
//get size of child2
file.read( (char*) &tempSize, sizeof(size_t));
//allocate memory for child2
parent* child2= (parent*) malloc(tempSize);
//read child 2 back
file.read( (char*) child2,tempSize);
file.close();
//Using virtual methods causes SEGFAULT
cout << "Recreated child" << child1->getName() << endl;
cout << "Recreated child" << child2->getName() << endl;
return 0;
}
并 Build 和运行如下:
g++ -g write.cpp -o write ; ./write
g++ -g read.cpp -o read ; ./read
当我逐步浏览gdb中的读取程序时,我注意到问题似乎是v表指针 . 当我在读取程序中重新创建“sarah”(child1)时,v表指针是写程序存在的指针,而不是读取程序 . 因此,可能这个写程序中“sarah”的v表指针指向导致SEGFAULT的无效内存区域 .
我有两个问题:
-
是否可以将v-table指针信息保存到“write”程序中的二进制文件中,以便在“正确”程序中完美地重新创建我的对象,而无需借助Boost :: Serialization或POST等库来处理这对我来说?
-
如果不可能......或者它非常复杂,那么我将不得不添加一个构造函数和一个“saveState()”方法(可以分别对ifstream和ofstream对象起作用),以便每个类(在此case sarah和bob)处理保存并从二进制文件中读取它的状态 . 这个问题是我有多个派生自类“parent”的类,所以我需要一种方法让“read”程序找出从读取二进制文件调用的构造函数 .
我想出了一种方法来确定要调用哪个构造函数 . 这将是
-
为每个派生自“父”的类提供唯一ID
-
在"write"程序中为二进制文件添加唯一ID
-
在“读取”程序中读取每个唯一ID,然后使用switch语句调用相关的构造函数 .
这不是很优雅,但每当我添加一个派生自“parent”的新类时,我必须给它一个ID并将其添加到“read”中的switch语句中 . 有没有更好的方法呢?
感谢阅读,我知道我的帖子很长!
1 回答
每次编译程序时,它都会将函数放在内存中的不同位置 . 此外,在某些操作系统配置中,每次重新启动程序时,功能甚至可能会移动 . 这是一种称为地址空间布局随机化的安全功能 . 如果您确定要从完全相同的二进制文件中读取和写入对象,则可以通过将读取和写入函数放在同一个程序而不是两个不同的程序中来执行您想要的操作 . 但是,即使这样也存在一个问题,即如果进行更改并重新编译,则无法再读取旧的数据文件 .
Boost::Serialization
是专门为避免所有这些问题而创建的,包括我甚至不知道,经过大量的同行评审和测试,并且具有非常自由的许可作为奖励 . 使用这样的库不是一件容易的事情,这是一种特权 .