|
Boost Users : |
Subject: [Boost-users] [serialization] Confusion serializing dynamic arrays of pointers.
From: John Murray (jmurrayufo_at_[hidden])
Date: 2009-05-04 13:54:28
Let me begin by saying that I am still rather new to C++ as a language. I've
only taken one low level university course on the language, and most of my
programs are still very limited in scope and functionality.
I am attempting to use the serialization library to save and load objects
from a dynamic array of pointers. The object to be serialized contains an
object***. The first pointer will bring me to an array of 10 elements, from
there each element contains an arbitrarily large array of pointers to
objects that will be dynamic allocated as they are needed.
While I understand that the library can handle simple arrays, as well as
pointers to objects, I cannot seem to get it work in this instance.
Attempting to serialize the pointer itself results in an error.
>In static member function 'static void
boost::serialization::access::serialize(Archive&, T&, unsigned int) [with
Archive = boost::archive::text_iarchive, T = spell**]':
>instantiated from 'void boost::serialization::serialize(Archive&, T&,
unsigned int) [with Archive = boost::archive::text_iarchive, T = spell**]'
>instantiated from 'void boost::serialization::serialize_adl(Archive&, T&,
unsigned int) [with Archive = boost::archive::text_iarchive, T = spell**]'
>instantiated from 'void boost::archive::detail::iserializer<Archive,
T>::load_object_data(boost::archive::detail::basic_iarchive&, void*,
unsigned int) const [with Archive = boost::archive::text_iarchive, T =
spell**]'
>functions/general.cpp:85: instantiated from here
This seems to make sense as there is not way for the library to serialize a
pointer without knowing what it is pointing to (and array of arrays in this
case).
My next choice of action was to try to serialize the pointer just as I had
created it during its initialization. My hope was that the library would
then be able to reconstruct the object during loading. This is shown in this
code.
class spellbook
{
public:
//Lots of functions ommited
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar,const unsigned int version){
ar & name;
ar & spellcounts;
ar & spellarraysize;
for(int x=0;x<10;x++){
for(int y=0;y < 10;y++){
ar & spellptr[x][y];
}
}
ar & target_level;
};
string name;
int spellcounts[10]; //! Each element of the array keeps track of the
number of spells in the spellptr
int spellarraysize[10]; //! Size of the array of the spells of each
level
spell*** spellptr; //! This should point to a 10xY array of spell
pointers. First array is of different levels, second of the spell pointers
themselves
int target_level; //! Shame to waste the memory, but needed to manage
archives
};
This compiles, but during run time i get the following error when I attempt
to load the object immediately after saving it to disk.
>terminate called after throwing an instance of
'boost::archive::archive_exception'
> what(): stream error
>Abort trap
I am fairly confident it is not the save/load archive code as I am currently
using what is more or less a duplicate of it to serialize other objects in
the file. I'd like to keep the currently implementation of pointers as it
keeps memory loads as dynamic to the needs of the application as possible.
Has anyone had any experience with serializing something of this nature, or
care to offer any helpful tips to get serializing and unserializing to work
with this situation?
I've ommited a large portion of the code as the headers get somewhat large
for this posting format. I can include links to the full source code if
needed.
John
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net