Boost logo

Boost Users :

Subject: Re: [Boost-users] boost serialization help
From: Robert Ramey (ramey_at_[hidden])
Date: 2011-09-04 13:26:15


ohad barta wrote:
> Hi
>
> My name is Ohad, and I work for the Technion institution, Israel.
> We are currently working on a research that involves programming in
> C++, and for that project we are using the serialization module of
> Boost.
> I'm not sure if this is the place to ask for help with boost. If not,
> Could you please direct me to the right address?
> my problems:
>
> 1) does the following supposed to work? :
> I have a :
> class saving helper{
> boost::archive::binary_archive* ar;
> }

I would not expect this to be used. If you think you need to pass around
a pointer to an archive - you've probably got something wrong in your
understand of how to use the library. I recommend spending some more
time looking at the examples and maybe some of the test code. Also
start with a small program which serializes one of your types and
incrementally expand it testing as you go. This makes it much
much easier, and much less painful, to become familiar with the package.
(Actually, I would offer this advice for all boost libraries)

> and:
> class Y {
> X* first
> vector <Y*> second
> }
>
> and I try to save Y with the saving helper:
> for each level of Y object I initialize a saving helper, directly
> call a function that does *ar & * first (the function name isnt
> serialize\save)
> for each Y* on the next level I create a new saving helper object
> with a new pointer, initiallized the same:
> saving_helper::saving_helper(saving_helper* to_copy){ this->ar =
> to_copy->ar;}
> and continue saving the very same object (another level) with the new
> saving helper
>
> (the load is standard - via a regualr load function)
>
> I know that this is not the standard way to use boost, but I dont
> have much choice.

When I see code like this, I'm thiinking that way too many pointers
are being used. I believe that many programmer use too many pointers
thus throwing away much of the efficient automatic memory management
which using member variables provides. If you look at your code above
it's totally impossible to understand by looking at it. You should re-think
it and see if you can re-craft so that it's obviously correct by inspection.

> 2) When trying to use the above method with text archive, I always
> get a streaming error, but If I use a binary archive than
> the streaming error vanished and instead I start to read the file
> from its middle (i.e. read the forth number instead of the first,
> reading the fifth bumber instead the second,etc).
> increasing the file length by adding to it 3 redundent numbers solved
> those two problems, and yet Im looking for a more stable solution, so
> help will be apprecialted.

The binary archive just saves and loads the bits of the data types without
actually "looking" at them. So it's likely to fail to detect some error
which
the usage of text archives might detect. One of the easiest errors to
commit
is to have the code in the save function not match the code in the load
function. The binary archive has no way to detect this. If you want to
check this, the easiest way is to serialize to an xml_archive which checks
the xml tags that they all match up so it detects a save/load mismatch
as soon as it occurs. After that, you can switch to the binary archive
which is much faster - but can't detect the errors.

> 3) boost tracking: where should I put the BOOST_CLASS_TRACKING macro?

I think it's too soon to worry about this. The default tracking should
be enough until you get to more specific/trickier situations.

Robert Ramey


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