Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      _Alignas(since C11)(deprecated in C23), alignas(since C23)

      From cppreference.com
      <c‎ |language
       
       
       
       

      Appears in thedeclaration syntax as one of the type specifiers to modify thealignment requirement of the object being declared.

      Contents

      [edit]Syntax

      _Alignas(expression) (1)(since C11)
      alignas(expression) (2)(since C23)
      _Alignas(type) (3)(since C11)
      alignas(type) (4)(since C23)
      expression - anyinteger constant expression whose value is a validalignment or zero
      type - anytype name

      The keyword_Alignas is also available as convenience macroalignas, available in the header<stdalign.h>.

      (until C23)

      [edit]Explanation

      The_Alignas(until C23)alignas(since C23) specifier can only be used when declaring objects that are notbit-fields, and don't have theregister storage class. It cannot be used in function parameter declarations, and cannot be used in a typedef.

      When used in a declaration, the declared object will have itsalignment requirement set to

      1,2) the result of theexpression, unless it is zero
      3,4) the alignment requirement oftype, that is, to_Alignof(type)(until C23)alignof(type)(since C23)

      except when this would weaken the alignment the type would have had naturally.

      Ifexpression evaluates to zero, this specifier has no effect.

      When multiple_Alignas(until C23)alignas(since C23) specifiers appear in the same declaration, the strictest one is used.

      _Alignas(until C23)alignas(since C23) specifier only needs to appear on thedefinition of an object, but if any declaration uses_Alignas(until C23)alignas(since C23), it must specify the same alignment as the_Alignas(until C23)alignas(since C23) on the definition. The behavior is undefined if different translation units specify different alignments for the same object.

      [edit]Notes

      In C++, thealignas specifier may also be applied to the declarations of class/struct/union types and enumerations. This is not supported in C, but the alignment of a struct type can be controlled by using_Alignas(until C23)alignas(since C23) in a member declaration.

      [edit]Keywords

      alignas,_Alignas

      [edit]Example

      Run this code
      #include <stdalign.h>#include <stdio.h> // every object of type struct sse_t will be aligned to 16-byte boundary// (note: needs support for DR 444)struct sse_t{    alignas(16)float sse_data[4];}; // every object of type struct data will be aligned to 128-byte boundarystruct data{char x;    alignas(128)char cacheline[128];// over-aligned array of char,// not array of over-aligned chars}; int main(void){printf("sizeof(data) = %zu (1 byte + 127 bytes padding + 128-byte array)\n",sizeof(struct data)); printf("alignment of sse_t is %zu\n", alignof(struct sse_t));     alignas(2048)struct data d;// this instance of data is aligned even stricter(void)d;// suppresses "maybe unused" warning}

      Output:

      sizeof(data) = 256 (1 byte + 127 bytes padding + 128-byte array)alignment of sse_t is 16

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C standards.

      DRApplied toBehavior as publishedCorrect behavior
      DR 444C11_Alignas was not allowed in struct and union membersallowed

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.7.5 Alignment specifier (p: TBD)
      • 6.2.8 Alignment of objects (p: TBD)
      • 7.15 Alignment <stdalign.h> (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.7.5 Alignment specifier (p: 92)
      • 6.2.8 Alignment of objects (p: 36-37)
      • 7.15 Alignment <stdalign.h> (p: 196)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.7.5 Alignment specifier (p: 127-128)
      • 6.2.8 Alignment of objects (p: 48-49)
      • 7.15 Alignment <stdalign.h> (p: 268)

      [edit]See also

      a type with alignment requirement as great as any other scalar type
      (typedef)[edit]
      _Alignof(until C23)alignof(since C23) queries alignment requirements of an object
      (operator)
      C++ documentation foralignas specifier
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/_Alignas&oldid=180144"

      [8]ページ先頭

      ©2009-2025 Movatter.jp