0

I need serialize class-field : a pointer to int I could not do it myself, please tell me.

class Person
{
 public:
    int age; // work
    int *ageptr = &age; // not work

    Person();
    ~Person();

private:
    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive &ar, const unsigned int version) {
        ar &BOOST_SERIALIZATION_NVP(age);
        ar &BOOST_SERIALIZATION_NVP(ageptr); // error
    }
};
Vitalja B.
  • 11
  • 6
  • Why would you want to do that? – LogicStuff May 28 '19 at 13:50
  • 3
    Most serialization libraries don't support pointers. In that case you need to just serialize the data and reconstruct a pointer to it after deserialization. Which means in your case: Don't serialize ageptr, just age and in your deserialization function deserialize age and then assign ageptr to &age. – nada May 28 '19 at 13:51
  • When you get an error, the first step is to read the error message. – eerorika May 28 '19 at 13:51
  • @nada, How can this be circumvented? I know that boost supports serialization of shared ptr – Vitalja B. May 28 '19 at 13:54
  • @LogicStuff, asked to do at the university – Vitalja B. May 28 '19 at 13:54
  • One issue with storing pointers is that the Operating System can load your program anywhere in memory and the memory area will not be the same. For example, let's say your pointer points to location 500. Next time your program runs, the available memory starts at 1000. When you read the value of 500 for the pointer, the pointer will be pointing to an invalid memory location (your program won't *own* that memory space, some other program will). – Thomas Matthews May 28 '19 at 13:55
  • Adding to what @ThomasMatthews said, it could be unserialized on a different machine with a different OS and different architecture! – Fred Larson May 28 '19 at 13:55
  • 3
    Don't store pointers; store the data that the pointers point to. For example, if there is a linked list of Person objects (with name and age), the name and age of each Person will be written to the file; the *next* and *previous* links won't be. The list will have to be reconstructed when the data is read in. – Thomas Matthews May 28 '19 at 13:55
  • @VitaljaB. If it is true that boost correctly serializes shared pointers, i suggest serializing your pointer as such and convert back, if you really need the raw pointer. – nada May 28 '19 at 13:56
  • @nada, I just need to serialize the pointer, show how it is, it will not be used anywhere else – Vitalja B. May 28 '19 at 13:56
  • @VitaljaB. sounds like either you're mis-understanding the requirements; or the professor has made a mistake - the need to serialise a pointer is, at best, niche. – UKMonkey May 28 '19 at 13:57
  • @VitaljaB. Look at the relevant SO questions [here](https://stackoverflow.com/questions/14974873/c11-stdshared-ptr-boostserialization) or [here](https://stackoverflow.com/questions/8115220/how-can-boostserialization-be-used-with-stdshared-ptr-from-c11). – nada May 28 '19 at 13:59
  • @nada, I do not know, I was asked to implement the serialization of the pointer to int. I have made all the other items except this one. Surprisingly, pointers to class object work correctly. – Vitalja B. May 28 '19 at 14:01
  • Serializing pointers has no seance. You can only serialize data pointed by that pointer. Pointers do not carry application data, but just memory structure. Note that during deserialization this specific memory address can be already occupied by something else so there is no way to restore that address. – Marek R May 28 '19 at 14:05

0 Answers0