Bug 70407 - alignment of array elements is greater than element size
Summary: alignment of array elements is greater than element size
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.8.4
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: documentation
Depends on:
Blocks:
 
Reported: 2016-03-25 01:33 UTC by Dehuan Xin
Modified: 2024-08-13 06:47 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-03-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dehuan Xin 2016-03-25 01:33:58 UTC
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.
Comment 1 Richard Biener 2016-03-29 09:09:28 UTC
Confirmed as a documentation issue.  Note that

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

works (and increases sizeof (S_)).
Comment 2 Dehuan Xin 2016-03-29 12:57:23 UTC
(In reply to Richard Biener from comment #1)
> Confirmed as a documentation issue.  Note that
> 
> typedef struct S_ { short f[3] __attribute((aligned(8))); } S;
> 
> works (and increases sizeof (S_)).

So for a struct, aligning the first field of it is effectively aligning the struct.

But that seems a variable attribute rather than type attribute, a very different usage. (as is documented here https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#Common-Variable-Attributes).

And for 

    typedef int more_aligned_int __attribute__ ((aligned (8)));

it's still an issue.