Next: , Up: Representation Clauses and Pragmas   [Contents][Index]


9.1 Alignment Clauses

GNAT requires that all alignment clauses specify a power of 2, and all default alignments are always a power of 2. The default alignment values are as follows:

An alignment clause may specify a larger alignment than the default value up to some maximum value dependent on the target (obtainable by using the attribute reference Standard'Maximum_Alignment). It may also specify a smaller alignment than the default value for enumeration, integer and fixed point types, as well as for record types, for example

type V is record
   A : Integer;
end record;

for V'alignment use 1;

The default alignment for the type V is 4, as a result of the Integer field in the record, but it is permissible, as shown, to override the default alignment of the record with a smaller value.

Note that according to the Ada standard, an alignment clause applies only to the first named subtype. If additional subtypes are declared, then the compiler is allowed to choose any alignment it likes, and there is no way to control this choice. Consider:

type R is range 1 .. 10_000;
for R'Alignment use 1;
subtype RS is R range 1 .. 1000;

The alignment clause specifies an alignment of 1 for the first named subtype R but this does not necessarily apply to RS. When writing portable Ada code, you should avoid writing code that explicitly or implicitly relies on the alignment of such subtypes.

For the GNAT compiler, if an explicit alignment clause is given, this value is also used for any subsequent subtypes. So for GNAT, in the above example, you can count on the alignment of RS being 1. But this assumption is non-portable, and other compilers may choose different alignments for the subtype RS.


Next: , Up: Representation Clauses and Pragmas   [Contents][Index]