While doing some experiment on serialization, I noticed that my object name is lost when the object is retrieved. Would you please show me what is going on?
void nDB::serialize(macro* myMacro) {
ofstream ar("macro.dat", ios::binary);
ar.write((char*)myMacro,sizeof(*myMacro));
}
macro* nDB::deserialize() {
macro* tmp_macro = (macro*)safemalloc(sizeof(macro));
ifstream ar("macro.dat", ios::binary);
ar.read((char*)tmp_macro,sizeof(*tmp_macro));
printf("My macro name is %s\n",tmp_macro->get_name());
return tmp_macro;
}
And this is what my output is
My macro name is \uffffs\uffff>
Thank you very much,
You are only storing the static memory for your object. Any dynamically allocated memory your object uses (such as
std::string,std::vectoror anything allocated withmalloc, ornew) will not be saved since they are located on the heap. You have to serialize and deserialize all dynamically allocated memory as well.