Next: , Previous: Null_Parameter, Up: Implementation Defined Attributes


The size of an object is not necessarily the same as the size of the type of an object. This is because by default object sizes are increased to be a multiple of the alignment of the object. For example, Natural'Size is 31, but by default objects of type Natural will have a size of 32 bits. Similarly, a record containing an integer and a character:

     type Rec is record
        I : Integer;
        C : Character;
     end record;

will have a size of 40 (that is Rec'Size will be 40. The alignment will be 4, because of the integer field, and so the default size of record objects for this type will be 64 (8 bytes).

The type'Object_Size attribute has been added to GNAT to allow the default object size of a type to be easily determined. For example, Natural'Object_Size is 32, and Rec'Object_Size (for the record type in the above example) will be 64. Note also that, unlike the situation with the Size attribute as defined in the Ada RM, the Object_Size attribute can be specified individually for different subtypes. For example:

     type R is new Integer;
     subtype R1 is R range 1 .. 10;
     subtype R2 is R range 1 .. 10;
     for R2'Object_Size use 8;

In this example, R'Object_Size and R1'Object_Size are both 32 since the default object size for a subtype is the same as the object size for the parent subtype. This means that objects of type R or R1 will by default be 32 bits (four bytes). But objects of type R2 will be only 8 bits (one byte), since R2'Object_Size has been set to 8.

Although Object_Size does properly reflect the default object size value, it is not necessarily the case that all objects will be of this size in a case where it is not specified explicitly. The compiler is free to increase the size and alignment of stand alone objects to improve efficiency of the generated code and sometimes does so in the case of large composite objects. If the size of a stand alone object is critical to the application, it should be specified explicitly.