satellite
satellite

Reputation: 13

Data structure alignment to implement serialize in C++

I understand the theory of data structure alignment and using this theory to implement serialize. However, it hard for me to get start on the real code. Could someone give me s simple example to show how this work?

Let's say:

 Class A {
  private:
     int a;
     char b;
     double c;
     char d;
     char e;
   public:
     void serialize(char * str);
 };

How to implement the serialize method to store a aligned data A? Should I just reorder the data structure and padding it? or...

Upvotes: 0

Views: 1339

Answers (1)

mavam
mavam

Reputation: 12572

Alignment and serialization are two orthogonal aspects.

The former concerns the in-memory representation of a data structure, whereas the latter relates to its equivalent representation in a sequential form. In other words, once a data structure is serialized into a stream of bytes, alignment is no longer relevant. Similarly, the notion of alignment does not make sense when referring to a stream of serialized bytes.

That said, you should address each issue separately rather than trying to find a joint solution.

Upvotes: 2

Related Questions