The keyword __alignof__
allows you to inquire about how an object is aligned, or the minimum alignment usually required by a type. Its syntax is just like sizeof
.
For example, if the target machine requires a double
value to be aligned on an 8-byte boundary, then __alignof__ (double)
is 8. This is true on many RISC machines. On more traditional machine designs, __alignof__ (double)
is 4 or even 2.
Some machines never actually require alignment; they allow reference to any data type even at an odd addresses. For these machines, __alignof__
reports the recommended alignment of a type.
When the operand of __alignof__
is an lvalue rather than a type, the value is the largest alignment that the lvalue is known to have. It may have this alignment as a result of its data type, or because it is part of a structure and inherits alignment from that structure. For example, after this declaration:
struct foo { int x; char y; } foo1;
the value of __alignof__ (foo1.y)
is probably 2 or 4, the same as __alignof__ (int)
, even though the data type of foo1.y
does not itself demand any alignment.
A related feature which lets you specify the alignment of an object is __attribute__ ((aligned (alignment)))
; see the following section.