This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/70407] New: alignment of array elements is greater than element size


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

            Bug ID: 70407
           Summary: alignment of array elements is greater than element
                    size
           Product: gcc
           Version: 4.8.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dxin at usc dot edu
  Target Milestone: ---

To verify, declare a POD type plus aligned attribute as a custom type.

    typedef struct S_ { short f[3]; } S __attribute__ ((aligned (8)));

Then make an array of such type.

    S test[16]={};

Then the code won't compile, with "error: alignment of array elements is
greater than element size".

Basically the `__attribute__ ((aligned (8)));` left the size of such type
unchanged, as opposed to including the alignment/padding in the size and
violates the C standard that `sizeof` operator returns the storage size plus
all necessary padding and maintain `sizeof(Type[N])==N*sizeof(Type)`.

This error is added in 2005 (
https://gcc.gnu.org/ml/gcc-patches/2005-09/msg01853.html ) to prevent GCC from
generating broken code, and is brought up again in a stackoverflow.com question
(http://stackoverflow.com/questions/36211864/how-can-i-apply-attribute-aligned32-to-an-int)
.

Note that clang-3.8 does include the extra padding as the size and allow
declaring arrays of extra-aligned types.

This problem is verified on GCC 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.1) and also
here https://ideone.com/oxGyiN .

If this bug is not to be fixed, please update the documentation
(https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#Common-Type-Attributes)
and mark it as a feature.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]