0

Is it possible to save an actual address stored in the pointer instead of the object that it points to?

I mean, boost::serialization automatically serializes the object referenced by a and not the address of the object but in my case there are a lot of references to the same object and I don't want to serialize them all over and over again, so I need to save just the address of such pointer.

1
  • Cast it to an integer? And also, as people have pointed out, you're doing things... wrong. If you want a unique ID, fine. Generate one. But storing the address in the file is misleading at the very least.
    – sehe
    Jun 8, 2017 at 8:49

1 Answer 1

2

That will not work. The memory location of an object at run time will vary from one execution of the program to the next.

What would work is some kind of object catalog or ID system, for example if an object had a built-in unique ID. The pointer address might even work for this if you only use it as an object ID.

Then (if you override normal serialization and deserialization) you could write each object once, read them the first time they are referenced and store the location into a map, then if the same ID is deserialized again you use the map to find its pointer.

This is probably not worth it unless you have a huge amount of duplication of a single type of object.

boost::archive::text_oarchive oa(ofs);
....
unsigned long long int foo = (unsigned long long int) &my_object;  // might need to lower warning level depending on compiler
oa << foo;

You should make sure boost is not already doing this for you. The boost.org tutorial on serialization means it only writes out objects referenced by pointer once: http://www.boost.org/doc/libs/1_64_0/libs/serialization/doc/tutorial.html#pointers

"If the same pointer is serialized more than once, only one instance is be added to the archive. When read back, no data is read back in. The only operation that occurs is for the second pointer is set equal to the first."

5
  • "The memory location of an object at run time will vary from one execution of the program to the next" -- yep, I'm fully aware of it. I'm going to use this address as some kind of a unique ID only Jun 7, 2017 at 3:05
  • So my question is still open. How can I serialize the address of the pointer? Jun 7, 2017 at 3:07
  • So yes, it is ok for me to deserialize a pointer that points to 0x12345 while 0x12345 is not available to me at the current state of the program. It's just a unique ID and I will change where it points to later. Jun 7, 2017 at 3:19
  • You could write it yourself, or assign it then serialize that. unsigned int 64 = &foo.
    – Dave S
    Jun 7, 2017 at 3:20
  • Sample added, and a reference to how boost already does this for some cases
    – Dave S
    Jun 7, 2017 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.