Boost logo

Boost Users :

Subject: Re: [Boost-users] serialization: const char*
From: Riccardo Murri (riccardo.murri_at_[hidden])
Date: 2010-12-08 09:45:39


Hi Hicham,

On Tue, Dec 7, 2010 at 6:24 PM, Hicham Mouline <hicham_at_[hidden]> wrote:
> serializing a const char* is not allowed I gather?
> char* seems not to be allowed either.  primitive types pointers are not serializable?
>

Serializing *pointer values* makes little sense (what use would be the
address, when you read it back in a different process?): you really
want to serialize whatever object they are pointing to. But then,
since pointers to non-class types are used in C to represent arrays,
there's no safe way to know how many pointed values should be
serialized, so Boost.Serialization errors out when you try to save
them into an archive.

There's an additional problem when serializing a variable-length
array: you must split the `serialization` method into save + load:
indeed, when you load a variable-len array from an archive, you must
allocate a destination buffer large enough to hold all the values,
which you don't when saving: the buffer is already there. The
attached code provides an example.

In the end: if you just want to serialize a string, use std::string
and serialization will do it for you out-of-the-box.

Best regards,
Riccardo




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