(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))
- Read a text file and make an object(
std::unordered_map
) that contains different types ofstd::vector<T>
s. The current timestamp is also recorded in the object. - Serialize the object and save it as a binary file, using the cereal library. - [file1]
- Repeat 1. (read text and make an object, writing the current timestamp - so the timestamps are different) - [object1]
- 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:
- Change
std::unordered_map
tostd::map
. -> Result doesn't change. - Run the program with debug mode. -> Result changes, but the two objects are still different.
- 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_back
ed 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);
}