Let's say, I have a struct RGB
and I want to create struct RGBA
, which inherits RGB
:
struct RGB {
unsigned char r;
unsigned char g;
unsigned char b;
};
struct RGBA: RGB {
unsigned char a;
};
Both will be used for reading uncompressed image data:
RGBA *pixel=static_cast<RGBA *>(image->uncompressed_data);
Question: Is this safe, regarding the memory layout of struct RGBA
? Does anyone guarantee, that:
unsigned char a
comes after theRGB struct
(not before)- There is no padding between
struct RGB
and the a parameter fromstruct RGBA
?
will #pragma pack
help here? It's all about memory layout during inheritance.
r
andg
. There is also no guarantee in the standard about the required alignment ofRGBA
. So even without having to answer the actual question you asked, this is not safe. – Threepiece