Suppose I have a struct, be it union’d or otherwise:
typedef struct {
union {
struct { float x, y, z; } xyz;
struct { float r, g, b; } rgb;
float xyz[3];
} notAnonymous;
} Vector3;
I’ve heard that some compilers automatically pad structs to enhance performance by creating word-aligned boundaries.
Presumably such synergy means the size of a struct cannot be guaranteed to be the sum of its component field sizes, and therefore there is a change of data corruption and/or overflow for array xyzs in the following:
inline Vector3 v3Make(float x, float y, float z) { Vector3 v = {x,y,z}; return v; }
float xyzs[6];
*(Vector3*)&xyzs[3] = v3Make(4.0f,5.0f,6.0f);
*(Vector3*)&xyzs[0] = v3Make(1.0f,2.0f,3.0f);
Correct?
It’s true the compiler can lay our your structure with what ever kind of padding it wants. You can use
#pragma packor__attribute__((packed))to avoid padding on most compilers. In practice, you have three 32-bit fields in there, so it’s probably not going to be a problem. You can check by usingsizeofon your structure type or a variable of that type and seeing what comes out.What is a problem is that you’re trying to assign a
Vector3to afloatvariable in your last two lines. That’s not going to be allowed. You could hack in what you’re trying to do:But that’s pretty ugly looking, not to mention confusing. It would be a lot better to change
xyzsto being an array ofVector3rather than of justfloat.