Suppose I have a structure, 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 heard that some compilers automatically compose structures to improve performance by creating word-aligned borders.
Presumably, this synergy means that the size of the structure cannot be guaranteed by the sum of the sizes of its components, and therefore, there is a change in data distortion and / or overflow for the array xyzsin 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);
Correctly?
source
share