Amumu
Amumu

Reputation: 18572

Why Serialization when a class object in memory is already binary (C/C++)?

My guess is that data is scattered in physical memory (even the data of a class object is sequential in virtual memory), so in order to send the data correctly it needs to be reassembled, and to be able to send over the network, one additional step is the transformation of host byte order to network byte order. Is it correct?

Upvotes: 10

Views: 1635

Answers (5)

Filip Roséen
Filip Roséen

Reputation: 63832

Proper serialization can be used to send data to arbitrary systems, that might not work under the same architecture as the source host.


Even an object that only consist of native types can be troublesome sharing between two systems because of the extra padding that might exists in between and after members, among other things. Sharing raw memory dumps of objects between programs compiled for the same architecture but with different compiler versions can also turn into a big hassle. There is no guarantee how variable type T actually is stored in memory.


If you are not working with pointers (references included), and the data is meant to be read by the same binary as it's dumped from, it's usually safe just to dump a raw struct to disk, but when sending data to another host.. drum roll serialization is the way to go.

I've heard developers talking about ntohl / htonl / ntohl / ntohs as methods of serializing/deserializing integers, and when you think about it saying that isn't that far from the truth.


The word "serialization" is often used to describe this "complicated method of storing data in a generic way", but then again; your first programming assignment where you were asked to save information about Dogs to file (hopefully*) made use of serialization, in some way or another.

* "hopefully" meaning that you didn't dump the raw memory representation of your Dog object to disk

Upvotes: 11

Louis
Louis

Reputation: 427

Pointer and data pack(data align)

If you memcpy your object's memory, there is dangerous to copy a wild pointer value instead of it's data. There is another risk, if the sender and receiver have different data pack(data align) method, you will get rubbish after decoding.

Upvotes: 2

harald
harald

Reputation: 6126

Binary representations may be different between different architectures, compilers and even different versions of the same compiler. There's no guarantee that what system A sees as a signed integer will be seen as the same on system B. Byte ordering, word langths, struct padding etc will become hard to debug problems if you don't properly define the protocol or file format for exchanging the data.

Upvotes: 1

fwg
fwg

Reputation: 1038

Pointers!

If you've allocated memory on the heap you'll just end up with a serialised pointer pointing to an arbitrary area of memory. If you just have a few ints and chars then yes you can just write it out directly to a file, but that then becomes platform dependent because of the byte ordering that you mentioned.

Upvotes: 7

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

Class (when we speak of C++) also includes virtual method pointers - and they must be reconstructed on receiving end.

Upvotes: 0

Related Questions