Possibility that objects are not the same after serialization/deserialization

66 Views Asked by At

(This question is not very clear, maybe not appropriate. I pondered a lot, but in the end, I thought it's better to get any advice than to ask nothing.)

I tried to serialize/deserialize a custom struct, using the 'cereal' library, but I found that some part of the deserialized object is not the same as the original object.

Here is the process I went through:
(Windows, MSVC, 'Release' configuration(optimization ON))

  1. Read a text file and make an object(std::unordered_map) that contains different types of std::vector<T>s. The current timestamp is also recorded in the object.
  2. Serialize the object and save it as a binary file, using the cereal library. - [file1]
  3. Repeat 1. (read text and make an object, writing the current timestamp - so the timestamps are different) - [object1]
  4. Deserialize the [file1] and compare it with [object1], by obj1.vector1 == obj2.vector1.

I thought only the vector that contains the timestamp would be different and all the other vectors would be the same, but some vectors were not the same.

What I tried:

  1. Change std::unordered_map to std::map. -> Result doesn't change.
  2. Run the program with debug mode. -> Result changes, but the two objects are still different.
  3. Fix the value of the timestamps. -> the two objects become the same.

=> Only the result of 3. is what I expected. I don't understand how this can happen. Any advice would be highly appreciated.


Below are some codes for describing what was done.

The struct contains some vectors, such as:

// not exactly the same struct that I used, but you get the idea..
struct MyStruct{
    std::vector<uint64_t> timestamps;
    std::vector<int> scores;
    std::vector<std::string> names;
    //...
}

(I push_backed the current timestamp in microseconds into the timestamps vector.)

Code used for serializing the object into a binary file using a standard way the library suggests:

{
    std::ofstream ofsb{ path, std::ios_base::binary };
    cereal::BinaryOutputArchive oab(ofsb); 
    oab(myObject_to_binary);
}

Code for deserialization, using the same 'cereal' library:

{
    std::ifstream ifsb{ path, std::ios_base::binary };
    cereal::BinaryInputArchive iab(ifsb);
    iab(myObject_from_binary);
}
0

There are 0 best solutions below