I have some values that I want to write into a raw byte array, in exactly defined locations, to send them off over the network.
//These are computed somewhere else in the program (and converted to little-endian regardless of platform):
using float32_t = float; static_assert(sizeof(float32_t) == 4);
uint32_t someInteger = 42;
float32_t someFloat = 4.2f;
bool someBoolean = true;
uint16_t someIndex = 42, someCRC1 = 42;
My first solution worked and produced the right result, but the memory sanitizer warned me about unaligned writes:
char* data = someBuffer.data(); //points directly into allocated raw bytes
(*reinterpret_cast<uint32_t *>(data )) = someInteger;
(*reinterpret_cast<bool *>(data + 4)) = someBoolean;
(*reinterpret_cast<float32_t*>(data + 5)) = someFloat; //unaligned
(*reinterpret_cast<uint16_t *>(data + 9)) = someIndex; //unaligned
(*reinterpret_cast<uint16_t *>(data + 11)) = someCRC; //unaligned
So I came up with this second solution by reordering some values and inserting a 1-byte buffer for alignment in between.
char* data = someBuffer.data(); //points directly into allocated raw bytes
(*reinterpret_cast<uint32_t *>(data )) = someInteger;
(*reinterpret_cast<float32_t*>(data + 4)) = someFloat;
(*reinterpret_cast<bool *>(data + 8)) = someBoolean;
(*reinterpret_cast<char *>(data + 9)) = '\0'; //buffer for memory alignment
(*reinterpret_cast<uint16_t *>(data + 10)) = someIndex;
(*reinterpret_cast<uint16_t *>(data + 12)) = someCRC; //this has to be the last value!
However I would much rather have the first ordering and no alignment bytes. Is that possible to do independently of platform or do I have to rely on having support for unaligned store instructions? I've heard that maybe using std::memcpy may be able to do this but I am not sure if that's true and how I would use it.