When the data is copied into char[]
buffer, it may not be properly aligned in memory for access as multi-byte types. Copying the data back into struct
restores proper alignment.
If I want to deserialize the individual values eg:
uint16_t len = 0;
memcpy(&len, buf, sizeof(len));
Assuming that you have copied the struct
into buf
, this is perfectly valid, because the language guarantees that the initial member would be aligned with the beginning of the structure. However, casting buf
to uint16_t*
is invalid, because the buffer many not be properly aligned in memory to be addressed as uint16_t
.
Note that getting elements of the struct other than the initial one require computing proper offset:
uint32_t seq;
memcpy(&seq, buf+offsetof(struct A, seq), sizeof(seq));
Also to deserialize the whole struct, if I just do
A tmp;
memcpy(&tmp, buf, sizeof(A));
Would this work fine or should I be worried about padding etc from the compiler?
This would work fine. Any padding embedded in the struct
when you copied it into the buf
would come back into tmp
, along with the actual data.