Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vector serialization

I am trying to binary serialize the data of vector. In this sample below I serialize to a string, and then deserialize back to a vector, but do not get the same data I started with. Why is this the case?

vector<size_t> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

string s((char*)(&v[0]), 3 * sizeof(size_t));

vector<size_t> w(3);
strncpy((char*)(&w[0]), s.c_str(), 3 * sizeof(size_t));

for (size_t i = 0; i < w.size(); ++i) {
    cout << w[i] << endl;
}

I expect to get the output

1  
2
3

but instead get the output

1
0
0

(on gcc-4.5.1)

like image 216
typedef Avatar asked Jul 05 '12 22:07

typedef


People also ask

Are vectors serializable?

A vector is only serializable if all the elements are also serializable. Or, you'll get a NotSerializableException thrown. 9.47.

What is the process of serialization?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is serialization in C++?

Serialization is the process of writing or reading an object to or from a persistent storage medium such as a disk file. Serialization is ideal for situations where it is desired to maintain the state of structured data (such as C++ classes or structures) during or after execution of a program.

What does serializing a model mean?

Serialization refers to the process of converting a data object (e.g., Python objects, Tensorflow models) into a format that allows us to store or transmit the data and then recreate the object when needed using the reverse process of deserialization.


2 Answers

The error is in the call to strncpy. From the linked page:

If the length of src is less than n, strncpy() pads the remainder of dest with null bytes.

So, after the first 0 byte in the serialized data is found the remainder of w's data array is padded with 0s.

To fix this, use a for loop, or std::copy

std::copy( &s[0], 
           &s[0] + v.size() * sizeof(size_t), 
           reinterpret_cast<char *>(w.data()) );

IMO, instead of using std::string as a buffer, just use a char array to hold the serialized data.

Example on ideone

like image 144
Praetorian Avatar answered Oct 05 '22 22:10

Praetorian


strncpy is a giant pile of fail. It will terminate early on your input because the size_t have some zero bytes, which it interprets as the NULL terminator, leaving them as default-constructed 0. If you ran this test on a BE machine, all would be 0. Use std::copy.

like image 40
Puppy Avatar answered Oct 05 '22 21:10

Puppy