[Bug c/36839] New: struct with only anonymous unions plus flexible array member

rtc at gmx dot de gcc-bugzilla@gcc.gnu.org
Tue Jul 15 16:33:00 GMT 2008


If I declare a struct with only anonymous unions plus a flexible array member,
gcc 4.2.3 complains about "flexible array member in otherwise empty struct"
Only if I add some other object to the struct the error goes away. removing the
flexible array member causes the error to go away, and sizeof reports the
expected size, which means that the struct is by no means "otherwise empty".

Here is an example:

cat > aufbug.c << EOF
#include <stdio.h>

int main(void)
{
        struct buggy {
                union {
                        int a;
                        char *b;
                };
#ifdef OTHER_MEMBER
                int x;
#endif
#ifdef FLEXIBLE
                char rest[];
#endif
        };
        printf("%zu\n", sizeof (struct buggy));

        return 0;
}
EOF

$ rm -f aufbug && make aufbug CFLAGS="-g3 -Wall -O2 -DFLEXIBLE" && ./aufbug
cc -g3 -Wall -O2 -DFLEXIBLE    aufbug.c   -o aufbug
aufbug.c: In function 'main':
aufbug.c:14: error: flexible array member in otherwise empty struct
make: *** [aufbug] Error 1

$ rm -f aufbug && make aufbug CFLAGS="-g3 -Wall -O2 -DFLEXIBLE -DOTHER_MEMBER"
&& ./aufbug
cc -g3 -Wall -O2 -DFLEXIBLE -DOTHER_MEMBER    aufbug.c   -o aufbug
8

$ rm -f aufbug && make aufbug CFLAGS="-g3 -Wall -O2" && ./aufbug
cc -g3 -Wall -O2    aufbug.c   -o aufbug
4


-- 
           Summary: struct with only anonymous unions plus flexible array
                    member
           Product: gcc
           Version: 4.2.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rtc at gmx dot de
 GCC build triplet: i486-pc-linux-gnu
  GCC host triplet: i486-pc-linux-gnu
GCC target triplet: i486-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36839



More information about the Gcc-bugs mailing list