A length clause specifies an amount of storage associated with a type.
length_clause ::= for attribute use simple_expression;
The expression must be of some numeric type and is evaluated during the elaboration of the length clause (unless it is a static expression). The prefix of the attribute must denote either a type or a first named subtype. The prefix is called T in what follows. The only allowed attribute designators in a length clause are SIZE, STORAGE_SIZE, and SMALL. The effect of the length clause depends on the attribute designator:
The expression must be a static expression of some integer type. The value of the expression specifies an upper bound for the number of bits to be allocated to objects of the type or first named subtype T. The size specification must allow for enough storage space to accommodate every allowable value of these objects. A size specification for a composite type may affect the size of the gaps between the storage areas allocated to consecutive components. On the other hand, it need not affect the size of the storage area allocated to each component.
The size specification is only allowed if the constraints on T and on its subcomponents (if any) are static. In the case of an unconstrained array type, the index subtypes must also be static.
The prefix T must denote an access type. The expression must be of some integer type (but need not be static); its value specifies the number of storage units to be reserved for the collection, that is, the storage space needed to contain all objects designated by values of the access type and by values of other types derived form the access type, directly or indirectly. This form of length clause is not allowed for a type derived from an access type.
The prefix T must denote a task type. The expression must be of some integer type (but need not be static); its value specifies the number of storage units to be reserved for an activation (not the code) of a task of the type.
The prefix T must denote the first named subtype of a fixed point type. The expression must be a static expression of some real type; its value must be greater than the delta of the first named subtype. The effect of the length clause is to use this value of small for the representation of values of the fixed point base type. (The length clause thereby also affects the amount of storage for objects that have this type).
A size specification is allowed for an access, task, or fixed point type, whether or not another form of length clause is also given for the type.
What is considered to be part of the storage reserved for a collection or for an activation of a task is implementation-dependent. The control afforded by length clauses is therefore relative to the implementation conventions. For example, the language does not define whether the storage reserved for an activation of a task includes any storage needed for the collection associated with an access type declared within the task body. Neither does it define the method of allocation for objects denoted by values of an access type. For example, the space allocated could be on a stack; alternatively, a general dynamic aliocation scheme or fixed storage could be used.
The objects allocated in a collection need not have the same size if the designated type is an unconstrained array type or an unconstrained type with discriminants. Note also that the allocator itself may require some space for internal tables and links. Hence a length clause for the collection of an access type does not always give precise control over the maximum number of allocated objects.
type MEDIUM is range 0 .. 65000; type SHORT is delta 0.01 range -100.0 .. 100.0; type DEGREE is delta 0.1 range -360.0 .. 360.0;
BYTE : constant := 8; PAGE : constant := 2000;
for COLOR'SIZE use 1*BYTE; -- see 3.5.1 for MEDIUM'SIZE use 2*BYTE; for SHORT'SIZE use 15;
for CAR_NAME'STORAGE_SIZE use -- approximately 2000 cars 2000*((CAR'SIZE/SYSTEM.STORAGE_UNIT) +1);
for KEYBOARD_DRIVERS'STORAGE_SIZE use 1*PAGE;
for DEGREE'SMALL use 360.0/2**(SYSTEM.STORAGE_UNIT -1);
Notes on the examples:
In the length clause for SHORT, fifteen bits is the minimum necessary, since the type definition requires SHORT'SMALL=2**(-7) and SHORT'MANTISSA = 14. The length clause for DEGREE forces the model numbers to exactly span the range of the type.
References: access type, allocator, allow, array type, attribute, collection, composite type, constraint, delta of a fixed point type, derived type, designate, elaboration, entity, evaluation, expression, first named subtype, fixed point type, index subtype, integer type, must, numeric type, object, real type, record type, small of a fixed point type, static constraint, static expression, static subtype, storage unit, subcomponent, system package, task, task activation, task specification, task type, type, unconstrained array type.