https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91672

            Bug ID: 91672
           Summary: wrong amount of storage allocated for initialized
                    structs with flexible array members
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Even though GCC takes advantage of tail padding when laying out structures with
flexible array members, in C mode it allocates excess storage for statically
initialized objects of such structs.

In the test case below, thanks to tail padding, sizeof (struct A) is
sufficiently large to store each of a0 through a3.  But as the assembly output
shows, the variables occupy more storage than necessary.

$ cat a.c && gcc -O2 -S -Wall -Wextra -o /dev/stdout -xc a.c | grep -e \.size
struct A
{
  __INT64_TYPE__ i64;
  __INT16_TYPE__ i16;
  __INT16_TYPE__ a16[];
};

struct A a0 = { 0, 1 };
struct A a1 = { 1, 1, { 1 } };
struct A a2 = { 2, 1, { 1, 2 } };
struct A a3 = { 3, 1, { 1, 2, 3 } };
struct A a4 = { 4, 1, { 1, 2, 3, 4 } };
        .size   a4, 24
        .size   a3, 22
        .size   a2, 20
        .size   a1, 18
        .size   a0, 16

G++ on the other hand, allocates the same amount of space regardless of the
size of the initializer:

$ gcc -O2 -S -Wall -Wextra -o /dev/stdout -xc++ a.c | grep -e \.size
        .size   a4, 16
        .size   a3, 16
        .size   a2, 16
        .size   a1, 16
        .size   a0, 16

Clang allocates just the right amount, i.e., 16 bytes for a0 through a3 and 24
bytes for a4.

Reply via email to