Bug 124003 - Using an aligned attribute in a typedef used for a field is not the same as indicating the attribute on the type of the field
Summary: Using an aligned attribute in a typedef used for a field is not the same as i...
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 15.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: documentation, wrong-code
Depends on:
Blocks:
 
Reported: 2026-02-06 13:12 UTC by Allan Blanchard
Modified: 2026-02-10 15:57 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Sample file reproducing the problem (226 bytes, text/x-csrc)
2026-02-06 13:12 UTC, Allan Blanchard
Details
Second example with introduced packed but there is still a strange behavior (232 bytes, text/x-csrc)
2026-02-09 14:07 UTC, Allan Blanchard
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Allan Blanchard 2026-02-06 13:12:47 UTC
Created attachment 63608 [details]
Sample file reproducing the problem

First of all, we are not sure that this is a bug. We found it while trying to replicate the behavior of GCC in an analyzer for C.

The sample file is attached.

The result that we get is:

field_t  : size 2 -- align 1
struct S1: size 3 -- align 1
struct S2: size 4 -- align 2
struct S3: size 3 -- align 1

We would have expected to be either:

field_t  : size 2 -- align 1
struct S1: size 3 -- align 1
struct S2: size 3 -- align 1
struct S3: size 3 -- align 1

or (even if it would be still surprising) :

field_t  : size 2 -- align 1
struct S1: size 4 -- align 2
struct S2: size 4 -- align 2
struct S3: size 3 -- align 1

It seems like when using an attribute in a typedef, if we use it for the type of a field, the attribute is attached to the field instead of being attached to the type, which is surprising.

Is it expected? If so, can someone explain the semantics of this code?

I found two issues that may be related, but I am not sure that it is the same:
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109824
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46354
Comment 1 Richard Biener 2026-02-09 07:28:28 UTC
The aligned attribute on types cannot decrease alignment without also specifying packed.  It can do that when placed on decls.  I didn't expect that to include 'typedef' decls.

The existing wording in the documentation might need some clarification for that I guess.

But - is the behavior expected?  I'm not sure...
Comment 2 Allan Blanchard 2026-02-09 13:43:23 UTC
Oh indeed, now that you tell it we should at least tell the user that they must specify packed if they want to reduce the alignment.
Comment 3 Allan Blanchard 2026-02-09 14:07:30 UTC
Created attachment 63634 [details]
Second example with introduced packed but there is still a strange behavior
Comment 4 Allan Blanchard 2026-02-09 14:09:09 UTC
I added a new example. This time, all structures are indicated as packed but instead of a short, I use an int that I try to align to 2. We get:

field_t  : size 4 -- align 2
struct S1: size 5 -- align 1
struct S2: size 6 -- align 2
struct S3: size 5 -- align 1

Which is still surprising.