Problems with char* serialization

Hello!
My name is Ariel and this is my first time with boost. I spent a few days
looking arround the serialization library, and right now I have a project
with my first serialized classes.
I spent some time reading the documentation and the tutorial. I am having
problems serializing pointers. I am trying to serialize a const char* and it
seems that doesn't work in the trivial way.
This is my code:
//-----------------------------------------------------------------------------
// myserializeclass.h
//-----------------------------------------------------------------------------
#include <fstream>
#include

serializing a pointer to a char is will only serialize one char.
try serializating either an array of characters or an std::string
Robert Ramey
"Ariel Brunetto"

My pointer to a char is actually an array. Is it possible to serialize it in
this way?
Ariel
On Tue, Apr 8, 2008 at 4:54 PM, Robert Ramey
serializing a pointer to a char is will only serialize one char.
try serializating either an array of characters or an std::string
Robert Ramey
"Ariel Brunetto"
wrote in message news:c1bd22250804081016m1bf9c734s143a08b9a166a2bb@mail.gmail.com... Hello! My name is Ariel and this is my first time with boost. I spent a few days looking arround the serialization library, and right now I have a project with my first serialized classes. I spent some time reading the documentation and the tutorial. I am having problems serializing pointers. I am trying to serialize a const char* and it seems that doesn't work in the trivial way.
This is my code:
//----------------------------------------------------------------------------- // myserializeclass.h
//----------------------------------------------------------------------------- #include <fstream> #include
#include #include //----------------------------------------------------------------------------- class MySerializeClass { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int uiVersion) { ar & m_pChar; } // members to serialize public: char* m_pChar; };
//----------------------------------------------------------------------------- BOOST_CLASS_TRACKING(MySerializeAbstractBase, ::boost::serialization::track_never)
//-----------------------------------------------------------------------------
//----------------------------------------------------------------------------- // myserializeclass.cpp
//----------------------------------------------------------------------------- int main() { MySerializeClass MySerializeClassObject;
std::ofstream ofs(akFilename); boost::archive::binary_oarchive oa(ofs);
oa << MySerializeClassObject; return 0; }
//-----------------------------------------------------------------------------
It doesn't compile. I get the following error:
1>c:\program files\microsoft visual studio 8\vc\platformsdk\include\boost\serialization\access.hpp(109) : error C2228: left of '.serialize' must have class/struct/union
I know that this is my mistake, but I don't know how to fix it ;). I know that there is a lot of consideration about pointer serialization. Could you point me in the right direction?
Thanks in advance! Ariel
------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ________________ Ariel Brunetto www.aquadize.com

binary_object will do it - but you have to create the array by hand.
look at using a reference to an array in stead of a pointer.
Robert Ramey
"Ariel Brunetto"

Sorry, but I dont understand. The documentation says that I can serialize a
pointer to a Serializable type such as a primitive type.
Is this true?
Ariel
On Tue, Apr 8, 2008 at 7:28 PM, Robert Ramey
binary_object will do it - but you have to create the array by hand.
look at using a reference to an array in stead of a pointer.
Robert Ramey
"Ariel Brunetto"
wrote in message news:c1bd22250804081247i4c34489fh5f69a0d0c8595b73@mail.gmail.com... My pointer to a char is actually an array. Is it possible to serialize it in this way? Ariel
On Tue, Apr 8, 2008 at 4:54 PM, Robert Ramey
wrote: serializing a pointer to a char is will only serialize one char.
try serializating either an array of characters or an std::string
Robert Ramey
"Ariel Brunetto"
wrote in message news:c1bd22250804081016m1bf9c734s143a08b9a166a2bb@mail.gmail.com... Hello! My name is Ariel and this is my first time with boost. I spent a few days looking arround the serialization library, and right now I have a project with my first serialized classes. I spent some time reading the documentation and the tutorial. I am having problems serializing pointers. I am trying to serialize a const char* and it seems that doesn't work in the trivial way.
This is my code:
//----------------------------------------------------------------------------- // myserializeclass.h
//----------------------------------------------------------------------------- #include <fstream> #include
#include #include //----------------------------------------------------------------------------- class MySerializeClass { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int uiVersion) { ar & m_pChar; } // members to serialize public: char* m_pChar; };
//----------------------------------------------------------------------------- BOOST_CLASS_TRACKING(MySerializeAbstractBase, ::boost::serialization::track_never)
//-----------------------------------------------------------------------------
//----------------------------------------------------------------------------- // myserializeclass.cpp
//----------------------------------------------------------------------------- int main() { MySerializeClass MySerializeClassObject;
std::ofstream ofs(akFilename); boost::archive::binary_oarchive oa(ofs);
oa << MySerializeClassObject; return 0; }
//-----------------------------------------------------------------------------
It doesn't compile. I get the following error:
1>c:\program files\microsoft visual studio 8\vc\platformsdk\include\boost\serialization\access.hpp(109) : error C2228: left of '.serialize' must have class/struct/union
I know that this is my mistake, but I don't know how to fix it ;). I know that there is a lot of consideration about pointer serialization. Could you point me in the right direction?
Thanks in advance! Ariel
------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ________________ Ariel Brunetto www.aquadize.com
------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ________________ Ariel Brunetto www.aquadize.com

Sorry, but I dont understand. The documentation says that I can serialize a pointer to a Serializable type such as a primitive type. Is this true?
Yes, but in case of pointer to const char your primitive type is const char. _________________________________________________________________ News, entertainment and everything you care about at Live.com. Get it now! http://www.live.com/getstarted.aspx

Yes, but I am trying to serialize a char*, not a const char*. Is it
possible?
Thank you,
Ariel
On Wed, Apr 9, 2008 at 4:17 AM, Igor R.
Sorry, but I dont understand. The documentation says that I can serialize a pointer to a Serializable type such as a primitive type. Is this true?
Yes, but in case of pointer to const char your primitive type is const char.
------------------------------ Get news, entertainment and everything you care about at Live.com. Check it out! http://www.live.com/getstarted.aspx
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ________________ Ariel Brunetto www.aquadize.com

Yes, but I am trying to serialize a char*, not a const char*. Is it possible?
Not directly. If you cannot reengineer the use of the char* do something like this: (apologies if you know this already)
// char* m_POD; a pointer to plain old char data
// size_t sizePOD; // how big is the POD data
//...
if (Archive::is_saving::value)
{
// create a vector and fill with POD
std::vector<char> vec(m_POD,m_POD+sizePOD);
// now serialize
ar & BOOST_SERIALIZATION_NVP(vec);
}
else if (Archive::is_loading::value)
{
std::vector<char> vec;
ar & BOOST_SERIALIZATION_NVP(vec);
// now handle deallocation of existing POD
// reallocation and copying from vec
if (m_POD)
{
delete m_POD;
}
// allocate POD buffer
m_POD = new char[vec.size()];
// copy from deserialized vector
std::copy(vec.begin(),vec.end(),m_POD);
}
HTH
----- Original Message -----
From: Ariel Brunetto
To: boost-users@lists.boost.org
Sent: Wednesday, April 09, 2008 11:59 AM
Subject: Re: [Boost-users] Problems with char* serialization
Yes, but I am trying to serialize a char*, not a const char*. Is it possible?
Thank you,
Ariel
On Wed, Apr 9, 2008 at 4:17 AM, Igor R.

Hi Ariel, if you want to use char* to deal with text, I'd recommend to use std::string instead, if you're interested in an array, you might want to consider using a std::vector. If you can't do this for some reason, here's some code which serializes a DREAL* (DREAL=double) and should work similarly for char*: friend class boost::serialization::access; template<class Archive> void save(Archive & ar, const unsigned int version) const { ar & num_labels; for (int i=0; i < num_labels; ++i) { ar & labels[i]; } } template<class Archive> void load(Archive & ar, const unsigned int version) { ar & num_labels; if (num_labels > 0) { labels = new DREAL[num_labels]; for (int i=0; i< num_labels; ++i) { ar & labels[i]; } } } BOOST_SERIALIZATION_SPLIT_MEMBER(); Cheers, Chris Ariel Brunetto wrote:
Yes, but I am trying to serialize a char*, not a const char*. Is it possible?
Thank you, Ariel On Wed, Apr 9, 2008 at 4:17 AM, Igor R.
mailto:igor_rubinov@hotmail.com> wrote: > Sorry, but I dont understand. The documentation says that I can serialize a pointer to a Serializable type such as a primitive type. Is this true?
Yes, but in case of pointer to const char your primitive type is const char.
------------------------------------------------------------------------ Get news, entertainment and everything you care about at Live.com. Check it out! http://www.live.com/getstarted.aspx
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org mailto:Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ________________ Ariel Brunetto www.aquadize.com http://www.aquadize.com ------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

First of all, thank to everyone you for the help.
What actually I want to do is to serialize large floats arrays, float* not
std::vector, but I could evaluate to use std::vector instead of float*.
Thanks a lot!
Ariel
On Wed, Apr 9, 2008 at 12:31 PM, Christian Widmer
Hi Ariel,
if you want to use char* to deal with text, I'd recommend to use std::string instead, if you're interested in an array, you might want to consider using a std::vector. If you can't do this for some reason, here's some code which serializes a DREAL* (DREAL=double) and should work similarly for char*:
friend class boost::serialization::access; template<class Archive> void save(Archive & ar, const unsigned int version) const { ar & num_labels; for (int i=0; i < num_labels; ++i) { ar & labels[i]; } }
template<class Archive> void load(Archive & ar, const unsigned int version) {
ar & num_labels; if (num_labels > 0) { labels = new DREAL[num_labels]; for (int i=0; i< num_labels; ++i) { ar & labels[i]; }
} }
BOOST_SERIALIZATION_SPLIT_MEMBER();
Cheers, Chris
Ariel Brunetto wrote:
Yes, but I am trying to serialize a char*, not a const char*. Is it possible?
Thank you, Ariel On Wed, Apr 9, 2008 at 4:17 AM, Igor R.
mailto:igor_rubinov@hotmail.com> wrote: > Sorry, but I dont understand. The documentation says that I can serialize a pointer to a Serializable type such as a primitive type. Is this true?
Yes, but in case of pointer to const char your primitive type is const char.
------------------------------------------------------------------------
Get news, entertainment and everything you care about at Live.com. Check it out! http://www.live.com/getstarted.aspx
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org mailto:Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ________________ Ariel Brunetto www.aquadize.com http://www.aquadize.com ------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ________________ Ariel Brunetto www.aquadize.com

you can but if you serialize a pointer to a character I don't think you'll get back what you want. Upon loading, the system will create a single new character and return a pointer to it. Somehow I don't think that's what you want.
Robert Ramey
"Ariel Brunetto"
participants (5)
-
Ariel Brunetto
-
Christian Widmer
-
Igor R.
-
Jerry
-
Robert Ramey