Where attribute((aligned)) is allowed to decrease alignment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi, 

I have been trying to work out what all the rules are for where
attribute((aligned(x))) can be used and where it can decrease alignment
and where it can only increase it.

The results I think I have found are:

Specifying aligned on a structure member can only increase alignment,
but if attribute((packed)) is specified also it can also decrease it.

Specifying aligned on a static variable can increase or decrease
alignment.

Specifying aligned on a stack variable can increase or decrease
alignment, but (at least on ARM) the platform may not allow misaligned
stack variables, and so even though _Alignof() returns 1, corresponding
misaligned access instructions are not generated.

Specifying aligned on a function argument is not allowed.

Specifying aligned on a pointer target is allowed but can only increase
alignment. 

Specifying aligned on a pointer target is not allowed when the pointer
itself is a function argument.

Specifying aligned on a typedef can increase or decrease alignment, and
the corresponding type can be used as a pointer target, even where the
pointer is a function argument.

This all seems very inconsistent.  Is there some systematic reason to it
all, or is it just historic?  Generating an error when the attribute is
on the target of a pointer only when the pointer is a function argument
really seems like a bug!

I found some details on this stack overflow question:
https://stackoverflow.com/questions/73152859/accessing-unaligned-struct-member-using-pointers

(although I am particularly interested in the case where no structs are
involved)

I also have experimented with various cases:
https://godbolt.org/z/73WPzEjYq

Does anyone have any light to shed on this all?

Many thanks,
Tom



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux