Boost logo

Boost Users :

From: Lorenzo Cavallaro (sullivan_at_[hidden])
Date: 2007-10-31 17:27:16


Hi,

   I'm trying to serialize the following piece of code (a data structure whose
   object size is dynamically allocated and it should use the correct size/len
   which has been serialized).

      typedef struct data {
         char *payload;
         size_t len;
      } data_t;

      vector<data_t *> v;

   So I added the corresponding serialize function to the structure definition

      void
      serialize(Archive & ar, const unsigned int file_version) {

         ar & len;
         for (int i = 0; i < len; i++)
            ar & payload[i];
      }

   Moreover, I added a default constructor which is invoked by the
   serialization library whenever the object has to be unserialized

      data::data() {
         /* no error checking */
         payload = (char *)calloc(1, X);
         len = X;
      }

   Now, of course X is fixed. I'd like to reuse the serialized 'size' (ar &
   size) as argument to the calloc to be able to create an object with the
   right size instead of having to approximate (exceeding, of course) its real
   dimension.

   Do you have any idea how to get it work the way I'd like to, please? -- if
   it's possible, of course.

   Nevertheless to say that with a right X everything works fine. I was
   thinking to iterate over all the elements, after the unserialization step
   is over, to reallocate payload using the right len... but it sucks :-)
   
TIA, bye
Lorenzo

-- 
Lorenzo `Gigi Sullivan' Cavallaro <sullivan_at_[hidden]>
GPG key at http://security.dico.unimi.it/~sullivan/sullivan.asc
Until I loved, life had no beauty;
I did not know I lived until I had loved. (Theodor Korner)
See the reality in your eyes, when the hate makes you blind. (A.H.X)

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