__alignof__ Keyword

Some processors align objects according to their type. The unary operator, __alignof__, determines the alignment of a specific type. By providing any type, this operator returns its alignment. This operator behaves in the same way as sizeof(type_name) operator. See the target backend section to check which alignment corresponds to which fundamental data type (if any is required) or to which aggregate type (structure, array).

This macro may be useful for the va_arg macro in stdarg.h, for example, to differentiate the alignment of a structure containing four objects of four bytes from that of a structure containing two objects of eight bytes. In both cases, the size of the structure is 16 bytes, but the alignment may differ, as shown in the following listing:

Listing: va_arg Macro
#define va_arg(ap,type)    \
  (((__alignof__(type)>=8) ? \

    ((ap) = (char *)(((int)(ap) \

    + __alignof__(type) - 1) & (~(__alignof__(type) - 1)))) \

    : 0), \

  ((ap) += __va_rounded_size(type)),\

  (((type *) (ap))[-1]))