pragma Pack (local_name);
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.
-- 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);
Exact data representationEdit
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. 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).
Although in Ada 83 packed boolean arrays were used for bit-wise operations, since Ada 95 modular types are more adequate for these operations. 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.
- 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.
- Adam Beneschan (2008-01-09). "Pragma Pack vs. Convention C, portability issue?". comp.lang.ada. (Web link). Retrieved on 2008-05-27.
- Software Productivity Consortium (October 1995). Ada 95 Quality and Style Guide, "10.5.7 Packed Boolean Array Shifts"
- Software Productivity Consortium (October 1995). Ada 95 Quality and Style Guide, "10.6.3 Bit Operations on Modular Types"
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.
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. Note that the packed attribute is not an standard feature of the C language, and cannot be used with arrays like in Ada.
- Ada Programming
- Ada Programming/Pragmas
- Ada Programming/Pragmas/Optimize
- Ada Programming/Pragmas/Implicit Packing (implementation defined)
- Ada Programming/Attributes/'Size
- Ada Programming/Attributes/'Component Size
- Ada Programming/Types/mod
Ada Reference ManualEdit
Ada 95 Quality and Style GuideEdit
Ada 83 RationaleEdit
- Free Software Foundation. "5.35 Specifying Attributes of Types — packed". Using the GNU Compiler Collection (GCC). http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html. 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."