You don't have to use a default contstructor. see load/save_construct data. Robert Ramey BTW - you might also look at binary_object for a fast saving a a long char array RR Lorenzo Cavallaro wrote:
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
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