I was scrolling through some posts and I read about something called the strict aliasing rule. It looked awfully close to some code I've seen in a club project, relevant snippet below.
LibSerial::DataBuffer dataBuffer;
size_t BUFFER_SIZE = sizeof(WrappedPacket);
while(true) {
serial_port.Read(dataBuffer, sizeof(WrappedPacket));
uint8_t *rawDataBuffer = dataBuffer.data();
//this part
auto *wrappedPacket = (WrappedPacket *) rawDataBuffer;
...
the struct definitions are:
typedef struct __attribute__((__packed__)) TeensyData {
int16_t adc0, adc1, adc2, adc3, adc4, adc5, adc6, adc7, adc8, adc9, adc10, adc11;
int32_t loadCell0;
double tc0, tc1, tc2, tc3, tc4, tc5, tc6, tc7;
} TeensyData;
typedef struct __attribute__((__packed__)) WrappedPacket {
TeensyData dataPacket;
uint16_t packetCRC;
} WrappedPacket;
Hopefully it's pretty obvious that I'm new to C++. So 1) is this a violation of the rule? and 2) if it is, what alternative solutions are there?