411 REFERENCE · Wikibooks
Ada Programming/Pragmas/Pack
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Pragmas Pack
__TOC__
pragma Pack (local_name);
Description
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. Sub-optimal code for packed boolean arrays — bug or inherent limitation?
Robert A. Duff
2007-07-09
comp.lang.ada
wccodim33u0.fsf@shell01.TheWorld.com
http://groups.google.es/group/comp.lang.ada/msg/685e3a9bc6dcf58d
2008-05-27
Example
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.
Pack (Packed_Bool_Array);
Incorrect usage
Portability
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
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. Free Software Foundation
Using the GNU Compiler Collection (GCC)
5.35 Specifying Attributes of Types — packed
http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
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. 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
Wikibook
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 Manual
13 2 Pragma Pack
Ada 95 Quality and Style Guide
10 6 3 Bit Operations on Modular Types
Ada 83 Rationale
15 4 1 Representation Pragmas 15 04
References
Pack
Pack