[Bug c/79269] New: Calculate size of struct with flexible array at compile time

mojo at world3 dot net gcc-bugzilla@gcc.gnu.org
Sun Jan 29 16:53:00 GMT 2017


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

            Bug ID: 79269
           Summary: Calculate size of struct with flexible array at
                    compile time
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mojo at world3 dot net
  Target Milestone: ---

As per the C99 standard, GCC treats structs with flexible arrays as if the
array had a size of 0. GCC offers an extension to allow such structs to be
initialized. For example:

struct {
    char a;
    char b[];
} test = { 10, { 0, 1, 2, 3 } };

In this case sizeof(test) will return 1, because b[] is considered to have a
size of zero even though GCC will correctly allocate 4 bytes and initialize
them.

To compliment this extension, a new builtin similar to sizeof() could be
created that returns the actual size of the struct (5 in this example).

This would be extremely useful for embedded systems in particular. For example,
one could:

struct {
    char length;
    char array[];
} test = { __new_sizeof(test) - 1, { 0, 1, 2, 3 } };


More information about the Gcc-bugs mailing list