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

            Bug ID: 118997
           Summary: Wrong struct padding or documentation is misleading
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincenzo.romano at gmail dot com
  Target Milestone: ---

Created attachment 60572
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60572&action=edit
ZIP archive containing: output from gcc -v, simple test code, *.i file

Consider this "non decorated" struct definition and variable declaration:

typedef struct _test {
  void* one;
  long double two[0];
} _test;

test _test;


sizeof(test) should evaluate as the same as sizeof(test.one), which is 8 on my
architecture.
Instead, it is reported as that of sizeof(long double), which is 16 on m my
architecture, while sizeof(test.two) yields 0.

The first example of the documentation at "6.18 Arrays of Length Zero" [1]
would actually allocate more memory than needed in case the argument
"this_length" evaluates to zero.

Either sizeof(test) is wrong or the referred documentation should mention that:

"The struct and size will be computed as if the zero-sized array was union-ed
to the preceding list of fields and being actually 1-sized".

Maybe other interpretations can better fit this behavior.

I am not sure which one would be better, while personally leaning towards a
solution where the results of the function sizeof() can be predicted by reading
the code and not the documentation.

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

Reply via email to