I am aware that using reinterpret_cast to cast an array of unsigned char to a struct pointer can cause problems because of padding and ordering of bytes on different systems (like in this example)
struct SomeData
{
unsigned char first;
int second;
};
unsigned char data[5];
// SomeData might be more than 5 bytes because of padding
// We can't be sure second is valid, because of bytes ordering
SomeData* someData = reinterpret_cast<SomeData*>(data);
But my question is for a struct having only unsigned char members
struct RGB
{
unsigned char r;
unsigned char g;
unsigned char b;
};
unsigned char data[3];
RGB* rgbData = reinterpret_cast<RGB*>(data);
In this case the struct RGB is kind of the equivalent of unsigned char[3], therefor I would assume there would be no padding. I have tested with g++ and msvc and no padding is added, is this guaranteed?
__attribute__((packed))- nada