Node: Pragma Pack for Arrays, Next: , Previous: Effect of Bit_Order on Byte Ordering, Up: Representation Clauses and Pragmas

Pragma Pack for Arrays

Pragma Pack applied to an array has no effect unless the component type is packable. For a component type to be packable, it must be one of the following cases:

For all these cases, if the component subtype size is in the range 1- 63, then the effect of the pragma Pack is exactly as though a component size were specified giving the component subtype size. For example if we have:

        type r is range 0 .. 17;
        type ar is array (1 .. 8) of r;
        pragma Pack (ar);

Then the component size of ar will be set to 5 (i.e. to r'size, and the size of the array ar will be exactly 40 bits.

Note that in some cases this rather fierce approach to packing can produce unexpected effects. For example, in Ada 95, type Natural typically has a size of 31, meaning that if you pack an array of Natural, you get 31-bit close packing, which saves a few bits, but results in far less efficient access. Since many other Ada compilers will ignore such a packing request, GNAT will generate a warning on some uses of pragma Pack that it guesses might not be what is intended. You can easily remove this warning by using an explicit Component_Size setting instead, which never generates a warning, since the intention of the programmer is clear in this case.

GNAT treats packed arrays in one of two ways. If the size of the array is known at compile time and is less than 64 bits, then internally the array is represented as a single modular type, of exactly the appropriate number of bits. If the length is greater than 63 bits, or is not known at compile time, then the packed array is represented as an array of bytes, and the length is always a multiple of 8 bits.