How to properly serialize raw binary data in C/C++?

1 week ago 5
ARTICLE AD BOX

What is the preferred way to serialize data to raw byte array (without any serialization libs)? I though these variants:

#pragma (pack,1) struct A{...} #pragma (pop) or __attribute((packed))__ allows to just memcpy the entire struct into the byte array, but has a great flaw of unaligned access to its fields, which is slow and will have compatibility issues between architectures.

Manually memcpy every field of the struct with corresponding offsets to the byte array. This doesn't mess with unaligned access at all. But looks very ugly.

Which way is preferred, especially in terms of performance and stability?

P.S. also would be helpful to get advice on deserialization, too.

Read Entire Article