411 REFERENCE · Wikibooks
Ada Programming/Attributes/'Size
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Attributes 'Size
__TOC__
Description
R'Size is a representation attribute used to get the number of bits of an object or type:
When applied to an object, 'Size yields the actual number of bits allocated to store the object.
When applied to a subtype, 'Size yields the smallest n such that all values fit in the range 0 .. 2n-1 for only positive values, else -2n-1 .. 2n-1-1.
The 'Size attribute may also be used in an attribute definition clause to set the size for a first subtype. In special cases, it is even possible to force a biased representation by using a smaller value than the n above.
Examples
This subtype allows a biased representation with only three bits because it comprises only eight values:
type T is range 1000 .. 1007;
for T'Size use 3;
Without the size clause, T'Size would return 10 because 210-1 = 1023.
attributes_size.adb
with Ada Text_IO;
procedure Attributes_Size is
package T_IO renames Ada.Text_IO;
package I_IO is new Ada.Text_IO.Integer_IO (Integer);
A_Boolean : constant Boolean := True;
begin
T_IO.Put ("Size of Boolean type = "); An enumeration with
I_IO.Put (Boolean'Size); 2 values fits into
T_IO.New_Line; 1 bit.
T_IO.Put ("Size of Boolean Object = "); it is more efficient
I_IO.Put (A_Boolean'Size); to store a boolean
T_IO.New_Line; as an entire byte
end Attributes_Size;
The output with GNAT 10.2.0 will be:
Size of Boolean type = 1
Size of Boolean Object = 8
Try it yourself and see how your compiler does.
The value of Size can also be specified using an attribute definition clause. For example, the following declarations specify a C99 compatible bool:
type Bool is new Boolean;
for Bool'Size use Interfaces C.int'Size;
Incorrect usages
See also
Wikibook
Ada Programming
Ada Programming/Attributes
Ada Programming/Attributes/'Component Size
Ada Programming/Attributes/'Max Size In Storage Elements
Ada Programming/Attributes/'Value Size (implementation defined)
Ada Programming/Attributes/'Object Size (implementation defined)
Ada 83 Reference Manual
13 7 2 Representation Attributes
A Predefined Language Attributes
Ada 95 Reference Manual
13 3 Operational and Representation Attributes
K Language-Defined Attributes
Ada 2005 Reference Manual
13 3 Operational and Representation Attributes
K Language-Defined Attributes
Ada 2012 Reference Manual
13 3 Operational and Representation Attributes
K Language-Defined Aspects and Attributes
Size
Size