[Bug c/79319] New: sizeof returns the wrong size of a union containing aligned members
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79319 Bug ID: 79319 Summary: sizeof returns the wrong size of a union containing aligned members Product: gcc Version: 6.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: erik at ehofman dot com Target Milestone: --- It looks like sizeof() returns an incorrect size if a union contains an aligned member: Consider the following code: #include typedef float vec4_t[4] __attribute__((aligned(32))); typedef union { float s4[4]; vec4_t v4; } vec4f_t; int main() { printf("float[4]: %zi\n", sizeof(float[4])); printf("vec4_t: %zi\n", sizeof(vec4_t)); printf("vec4f_t: %zi\n\n", sizeof(vec4f_t)); } when running it prints: __m128: 16 vec4_t: 16 vec4f_t: 32 As you can see both union members are 16-bytes but when quiring the size of the union it returns the size of the alignment (change it to aligned(64) and the size becomes 64).
[Bug c/79319] sizeof returns the wrong size of a union containing aligned members
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79319 Erik Hofman changed: What|Removed |Added Status|RESOLVED|CLOSED --- Comment #2 from Erik Hofman --- Ah okay I hadn't thought of that. It's still confusing when encountering it for the first time.. Thanks for the answer.
[Bug c/79319] sizeof returns the wrong size of a union containing aligned members
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79319 --- Comment #4 from Erik Hofman --- This was just the shortest snippet of code that showed the situation. The reason for 32-byte alignment is that I use it with AVX code and wanted the fastest possible assignment from a float vector.
[Bug c/79319] sizeof returns the wrong size of a union containing aligned members
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79319 --- Comment #6 from Erik Hofman --- Thanks for the suggestion but after thinking it through some more I came to the conclusion I made a mistake. 32-byte alignment is only required for AVX when using 8-float (or 4-double) vectors. Otherwise the compiler would switch back to SSE which doesn't requite 32-byte alignment. So 32-byte alignment is required only when processing large arrays of floats. Sorry for the fuzz.