Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure member alignment with _Alignas

Tags:

c

standards

c11

I was wondering about the following: is the new _Alignas alignment specifier in C11 applicable to structure members?

I've always assumed that much, but a thorough reading of the N1570 public draft seems to indicate that an alignment-specifier cannot appear in a specifier-qualifier-list, which is where I'd expect it to be, if it were supported. I've read the grammar a couple of times but can't figure out how _Alignas is supposed to be permitted in a structure member declaration.

However, it seems to me that the intent of the standard is that _Alignas should be applicable to structure members as the paragraph on _Alignas (§ 6.7.5) states that "an alignment attribute shall not be specified in a declaration of [...] a bit-field". Given that the term "bit-field" is defined in § 6.7.2.1 to be a structure member (precise wording: "such a member is called a bit-field"), I had always interpreted that sentence to implicitly mean alignment specifiers were allowed for non-bit-field members.

Checking against existing implementations shows that both Clang 3.0 and GCC 4.7 support _Alignas on structure members without complaining (with -pedantic). The Clang source code reproduces the same grammar from N1570, except Parser::ParseSpecifierQualifierList allows alignment specifiers; the function does contain a TODO element, though, that reads:

/// TODO: diagnose attribute-specifiers and alignment-specifiers.

The GCC C parser code appears to be similar, i.e. even though it quotes the standard grammar, it allows alignment specifiers in specifier-qualifier lists.

I've also checked the list of known defects, as well as comp.lang.c and comp.std.c, to see if the topic had been raised there but it doesn't appear to be the case. Hence, my question: are alignment specifiers supposed to be allowed on structure members?

EDIT: The relevant grammar rules are:

// Compare this...
(6.7) declaration-specifiers:
           storage-class-specifier declaration-specifiers_opt
           type-specifier declaration-specifiers_opt
           type-qualifier declaration-specifiers_opt
           function-specifier declaration-specifiers_opt
           // This seems to be the only place that mentions
           // alignment-specifier on the rhs of a rule.
           alignment-specifier declaration-specifiers_opt

(6.7.2.1) struct-or-union-specifier:
           struct-or-union identifier_opt { struct-declaration-list }
           struct-or-union identifier

(6.7.2.1) struct-declaration-list:
           struct-declaration
           struct-declaration-list struct-declaration

(6.7.2.1) struct-declaration:
           specifier-qualifier-list struct-declarator-list_opt ;
           static_assert-declaration

// ...to this.
(6.7.2.1) specifier-qualifier-list:
           type-specifier specifier-qualifier-list_opt
           type-qualifier specifier-qualifier-list_opt
           // Missing alignment-specifier specifier-qualifier-list_opt?

(6.7.5) alignment-specifier:
          _Alignas ( type-name )
          _Alignas ( constant-expression )
like image 859
rz0 Avatar asked May 20 '12 20:05

rz0


People also ask

What do you mean by structure member alignment padding and data packing?

Structure packing is only done when you tell your compiler explicitly to pack the structure. Padding is what you're seeing. Your 32-bit system is padding each field to word alignment. If you had told your compiler to pack the structures, they'd be 6 and 5 bytes, respectively.

What does 4 byte aligned mean?

For instance, in a 32-bit architecture, the data may be aligned if the data is stored in four consecutive bytes and the first byte lies on a 4-byte boundary. Data alignment is the aligning of elements according to their natural alignment.

What is the purpose of Alignas?

The alignas type specifier is a portable, C++ standard way to specify custom alignment of variables and user defined types. The alignof operator is likewise a standard, portable way to obtain the alignment of a specified type or variable.

Can compiler reorder struct members?

Since the rules are fixed in the language, the compiler is able to figure out how the members were reordered, and react accordingly. As mentioned above, it will always be possible to prevent reordering in the cases where you want complete control.


1 Answers

Section 6.7.5, p 6, clearly specifies that this also concerns alignment of members

The alignment requirement of the declared object or member is taken to be the specified alignment.

So the intended semantic is that one. If as you say the formal specification of the grammar misses that bit (I didn't check), this is a defect, and you should report it.

Edit: Looking into the grammar, it seems to me that an addition of alignment-specifier in 6.7.2.1 is missing in the cases of specifier-qualifier-list, and also that a textual explanation in para 14 would be in order.

like image 181
Jens Gustedt Avatar answered Oct 13 '22 00:10

Jens Gustedt