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 <stdio.h>
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).

Reply via email to