Why Serialization when a class object in memory is

2020-07-06 07:39发布

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?

5条回答
等我变得足够好
2楼-- · 2020-07-06 08:04

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

查看更多
Bombasti
3楼-- · 2020-07-06 08:05

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.

查看更多
太酷不给撩
4楼-- · 2020-07-06 08:22

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-07-06 08:22

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

查看更多
何必那么认真
6楼-- · 2020-07-06 08:28

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.

查看更多
登录 后发表回答