Boost logo

Boost Users :

Subject: Re: [Boost-users] serialization: const char*
From: Matthias Troyer (troyer_at_[hidden])
Date: 2010-12-09 09:40:55


On Dec 8, 2010, at 9:45 AM, Riccardo Murri wrote:

> 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?

Indeed, primitive type pointers are not serializable. This is a design decision in Boost.Serialization. Unless you want to serialize a pointer to a single char read Riccardo's mail below. If you indeed need to send a pointer to a single char then you will need to wrap that char into a struct, and write a serialization function for it.

>>
>
> 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.


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