Ada Programming/Attributes/'Size

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Ada Lovelace 1838.jpg

Contents


[edit] 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 types, 'Size yields the minimum number of bits required to store an object of that type.

Thus, the 'Size of a type might be lower than the 'Size of its objects. This is especially important to note for variant records, where a particular instance of the record may be much larger than the smallest possible record instance.

The 'Size attribute may also be used to set the minimum size in bits used to store a type.

[edit] Example

File: attributes_size.adb (view, plain text, download page, browse all)
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 3.4.1 will be:

Size of Boolean type   =           1
Size of Boolean Object =           8

Try it yourself and see how your compiler does.

[edit] Incorrect usages

A common Ada programming mistake is to assume that 'Size forces the compiler to use exactly the number of bits specified to store objects. This is not true. Using 'Size will force the compiler to use a minimum size, but the compiler is still free to store objects in bit sizes larger than specified.

[edit] See also

[edit] Wikibook

[edit] Ada 83 Reference Manual

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual