Ada Programming/Pragmas/Pack

Ada. Time-tested, safe and secure.
Ada. Time-tested, safe and secure.
pragma Pack (local_name);

Description edit

Pack is a representation pragma that directs the compiler to use type representations that favor conservation of storage space, rather than ease of access. It can be used with composite types, like arrays and records. Using pragma Pack will usually result in smaller data structures at the cost of additional operations to retrieve each element (bit operations may be required in order to retrieve packed data). This may or may not result in a loss of execution time, since the increase of instructions executed to access each element can be cheaper than the cost to read an unpacked data structure from memory.[1]

Example edit

-- A boolean is typically stored in a byte by the compiler, not a bit
-- An array of eight booleans will therefore occupy 64 bits, not 8!
type Packed_Bool_Array is array (1 .. 8) of Boolean;
-- With pragma pack, the compiler will try to compress the entire array,
-- in this case probably into a single byte (but this is not guaranteed).
-- Accessing one of these booleans, however, will require bit operations.
pragma Pack (Packed_Bool_Array);

Incorrect usage edit

Exact data representation edit

It is important to realize that pragma Pack must not be used to specify the exact representation of a data type, but to help the compiler to improve the efficiency of the generated code.[2] The compiler is free to ignore the pragma, therefore if a specific representation of a type is required, representation clauses should be used instead (record representation clauses, and/or attributes 'Size or 'Component_Size).

Bit-wise operations edit

Although in Ada 83 packed boolean arrays were used for bit-wise operations,[3] since Ada 95 modular types are more adequate for these operations.[4] The argument may be weighed against the advantages of named Boolean array indexes such as Traffic_Lights'(Red => True, others => False), depending on use case.

Portability edit

The pragma Pack is standard in the language since Ada 83. However, implementations are allowed to produce different results or to ignore it completely, and therefore it is not portable between compilers. It can produce different results depending on the target architecture too. Thus the pragma Pack should not be used when an exact representation is required for a type.

Interfacing edit

There is no equivalent standard feature in other programming languages as far as the authors know, so this pragma should not be used when interfacing.

GCC provides an extension to the C programming language that can be used to pack structs, unions (equivalent to Ada's records) or enumeration types.[5] Note that the packed attribute is not a standard feature of the C language, and cannot be used with arrays like in Ada.

See also edit

Wikibook edit

Ada Reference Manual edit

Ada 95 Quality and Style Guide edit

Ada 83 Rationale edit

References edit

  1. Robert A. Duff (2007-07-09). "Sub-optimal code for packed boolean arrays — bug or inherent limitation?". comp.lang.ada. (Web link). Retrieved on 2008-05-27.
  2. Adam Beneschan (2008-01-09). "Pragma Pack vs. Convention C, portability issue?". comp.lang.ada. (Web link). Retrieved on 2008-05-27.
  3. Software Productivity Consortium (October 1995). Ada 95 Quality and Style Guide, "10.5.7 Packed Boolean Array Shifts"
  4. Software Productivity Consortium (October 1995). Ada 95 Quality and Style Guide, "10.6.3 Bit Operations on Modular Types"
  5. Free Software Foundation. "5.35 Specifying Attributes of Types — packed". Using the GNU Compiler Collection (GCC). Retrieved 2008-11-23. "packed: This attribute, attached to struct or union type definition, specifies that each member (other than zero-width bitfields) of the structure or union is placed to minimize the memory required. When attached to an enum definition, it indicates that the smallest integral type should be used."