comp.lang.ada
 help / color / mirror / Atom feed
* Pragma pack
@ 1997-03-10  0:00 C. Berg (602) 436-5290
  1997-03-11  0:00 ` Robert Dewar
  1997-03-17  0:00 ` Matthew Heaney
  0 siblings, 2 replies; 4+ messages in thread
From: C. Berg (602) 436-5290 @ 1997-03-10  0:00 UTC (permalink / raw)



I have some questions for the list Ada gods regarding the use of
pragma pack (this is from Ada '83 land).

If you have a record similar to the following:

 type blah is record
   field_1 : field_1_type;
   field_2 : field_2_type;
   <etc...>
 end record;

What is the difference (to an individual record like this) between
employing a pragma pack on this record alone, without packing its
component fields, and not using a pack at all?  For example, will
a variable declared to be of this record type be the exact same size
with only the "highest" level pack as with no packs at all?

Also, while I would assume that a 'Size attribute will dynamically
reflect the effect of pragma pack on a type, can anybody confirm to me
that this is actually true, and does the same go for the USE clause?

I realize that implementations of pragmas are more or less up the
compiler writers, so any input, rules of thumb, references, etc,
are welcome....

Thanks in advance,

- Cory
(any expressed opinions are mine alone, disclaimer, disclaimer....)




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Pragma pack
  1997-03-10  0:00 Pragma pack C. Berg (602) 436-5290
@ 1997-03-11  0:00 ` Robert Dewar
  1997-03-17  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1997-03-11  0:00 UTC (permalink / raw)



Cory said

<<What is the difference (to an individual record like this) between
employing a pragma pack on this record alone, without packing its
component fields, and not using a pack at all?  For example, will
a variable declared to be of this record type be the exact same size
with only the "highest" level pack as with no packs at all?

Also, while I would assume that a 'Size attribute will dynamically
reflect the effect of pragma pack on a type, can anybody confirm to me
that this is actually true, and does the same go for the USE clause?

I realize that implementations of pragmas are more or less up the
compiler writers, so any input, rules of thumb, references, etc,
are welcome....>>

The last paragraph is very wrong. The Ada 95 reference manual is
pretty explit, and in fact answers, quite clearly, the first two
questions, especially if you assume that IA is followed.

The answer to the first question is that the pack at the higher level
definitely has an effect. For example, a record of 8 booleans should
take 8 bits if the record is packed, but may well take 64-bits if
it is not packed.

Packig a record has no effect on the representation of components (this
is IA, but is followed by GNAt and other Ada 95 compilers as far as I
know). So if you want the components individually packed, that must
be done separately.

Yes, of course the 'Size value will reflect the result of such packing.

I suggest rereading the 95 RM carefully in this area!





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Pragma pack
  1997-03-17  0:00 ` Matthew Heaney
@ 1997-03-17  0:00   ` Robert Dewar
  0 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1997-03-17  0:00 UTC (permalink / raw)



Matthew Heaney says

<<If you're just optimizing for space, then you can pragma Pack the record.
But if you require a specific representation, then you must use a
representation clause.>>

Or, where appropriate, a pragma Convention, which is far more portable.
Also, if you want to save space, it is important to pack any individual
composite components separately. For example

   type x is array (0..7) of Boolean;
   type y is record
     a,b,c,d : x;
   end record;

   pragma pack (y);

Here using GNAT, and quite likely with other compilers too, the size of
y will be 256 bits. If you want 32 bits, you must also write:

   pragma pack (x);

and it is actually probably this second pack that actually does the job,
the pragma pack of y is probably redundant (but certainly harmless!)

Note that in some Ada 83 compilers, you could achieve the tight packing
simply by saying

  for y'size use 32;

but both Ada 83 AI's and the implementation advice in the Ada 95 RM advise
against allowing 'Size to influence internal representations, and GNAT
follows this advice, so it will reject this size clause in the absence
of a pragma pack for type x.

Similarly

  for x'size use 8

cannot be used to effect the packing of x, either

  pragma Pack (x)

or

  for x'component_size use 1;

is required. Putting both in, as well the size clause, again is probably
redundent, but harmless. Over-specification does no harm in the world
of rep clause non-portability :-)





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Pragma pack
  1997-03-10  0:00 Pragma pack C. Berg (602) 436-5290
  1997-03-11  0:00 ` Robert Dewar
@ 1997-03-17  0:00 ` Matthew Heaney
  1997-03-17  0:00   ` Robert Dewar
  1 sibling, 1 reply; 4+ messages in thread
From: Matthew Heaney @ 1997-03-17  0:00 UTC (permalink / raw)



In article <01IGC5R855Z6001KU2@EMAIL.AZ.HONEYWELL.COM>, "C. Berg (602)
436-5290" <BERGC@ELLX.DECNET.AZ.HONEYWELL.COM> wrote:

>I have some questions for the list Ada gods regarding the use of
>pragma pack (this is from Ada '83 land).
>
>If you have a record similar to the following:
>
> type blah is record
>   field_1 : field_1_type;
>   field_2 : field_2_type;
>   <etc...>
> end record;
>
>What is the difference (to an individual record like this) between
>employing a pragma pack on this record alone, without packing its
>component fields, and not using a pack at all?  For example, will
>a variable declared to be of this record type be the exact same size
>with only the "highest" level pack as with no packs at all?

The idea is that pragma Pack remove the gaps between the components; it has
nothing to say about the components.

It's more clear thinking about a packed array: the gaps between the
components are "minimized," but the pragma has no effect on the components
themselves.  (You'll require separate representation clause for the array
components.)

If you're just optimizing for space, then you can pragma Pack the record. 
But if you require a specific representation, then you must use a
representation clause.

And of course, the use of pragma Pack definately _does_ affect the size of
the record.

(Note that in the case of a packed array of components whose size is 1, the
RM does guarantee that the bits occupy contiguous bit positions; ie there
are no gaps.  This is the only time when pragma Pack specifies a
represention.)

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1997-03-17  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-03-10  0:00 Pragma pack C. Berg (602) 436-5290
1997-03-11  0:00 ` Robert Dewar
1997-03-17  0:00 ` Matthew Heaney
1997-03-17  0:00   ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox