I have been researching a bit about efficient methods of serialization and deserialization of objects in C++. I am writing for an embedded system so I am wondering if the following is valid?
static const uint8_t data[] = {0xAA, 0xBB, 0x10, 0b00000101, 0x0A, 0x00, 0xDE, 0xAD, 0xBE, 0xEF, 0x04, 0x00, 0x00, 0x11, 0x22, 0x33};
struct __attribute__((__packed__)) Message {
uint8_t std[2];
uint8_t id;
union {
struct {
bool optionsReply : 1;
bool optionsFoo : 1;
bool optionsBar : 1;
};
uint8_t optionsU8;
};
uint16_t payloadLength;
uint8_t payload[0];
};
struct __attribute__((__packed__)) TestMessage : public Message {
uint8_t a0;
uint8_t a1;
uint8_t a2;
uint8_t a3;
uint16_t length;
uint8_t data[0];
};
const Message* msg = reinterpret_cast<const Message*>(data);
const TestMessage* testMsg = static_cast<const TestMessage*>(msg);
I tested this with my compiler, and it looks like everything works as I would expect. My question is, because of zero length arrays that I am forcing to be larger at runtime via the associated length, is this the preferred/safest method of doing this?
Thanks