3

Is it possible in C++ to serialize an object by taking a pointer of the first address of the object and increment this pointer till the end of the object is reached?

If it's possible, how can I find the first memory adress of the object and in which data type should I store the values? And how could I build the object on the other side?

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
user1073834
  • 103
  • 4
  • why do you want to serialize. is it to send the object to another process or computer, or so you can write it to a file? – Scott Langham Nov 30 '11 at 17:11
  • 2
    Here are some things you'll need to consider: (1) how to deal with classes that have non-trivial constructors; (2) how to deal with pointers/references contained within the object; (3) how to deal with virtual base classes. – NPE Nov 30 '11 at 17:12
  • maybe sending it over a tcp socket or something else. – user1073834 Nov 30 '11 at 17:14

3 Answers3

3

This is relatively easy to do in some very restrictive circumstances (POD with no pointers/references; same OS, CPU architecture and the C++ compiler on both ends of the serialization pipe).

There is a number of issues that complicate matters in the more general case:

  1. dealing with classes that have non-trivial constructors/destructors;
  2. dealing with pointers/references contained within the object;
  3. cycles in the pointer/reference graph;
  4. polymorphism;
  5. dealing with virtual base classes;
  6. endianness;
  7. field alignment, padding etc;
  8. the width of primitive types (int on one platform isn't necessarily the same size as int on another);
  9. versioning so that new fields can be added without breaking stuff.
Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 3
    Even in a POD containing no pointers or references it can fail due to padding between fields. – Jerry Coffin Nov 30 '11 at 17:21
  • @Jerry: depends what aix means by, "same OS, CPU architecture and the C++ compiler". If the two implementations share a C ABI, then they'll necessarily have the same layout for POD structs. But compiler options can affect that. – Steve Jessop Nov 30 '11 at 17:38
  • @SteveJessop: it's pretty common to use things like `#pragma pack` to force a specific alignment for structs used in the ABI, but still have variation in other structs, so sharing an ABI doesn't necessarily mean much. – Jerry Coffin Nov 30 '11 at 18:00
2

The procedure you describe would create a flat copy of the object. If that object contained pointers or references itself, this scheme would break.

If you want to serialize objects, use a library like Boost.Serialization.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • And even if it doesn't it's completely non-portable as it exposes implementation details like alignment, padding, endian and the sizes of types. – Laurence Gonsalves Nov 30 '11 at 17:14
0

There are several serialization formats (XDR, ASN1, JSON, YAML), techniques, and libraries (like s11n for C++)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547