comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Packing Record Structures in Ada
Date: 1998/01/12
Date: 1998-01-12T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680001201982350020001@news.ni.net> (raw)
In-Reply-To: 884639188.24707084@dejanews.com


In article <884639188.24707084@dejanews.com>, Randall_Rathbun@rc.trw.com wrote:

>Has anyone struggled and fixed the following problem with ordering inside
>the Ada83 record structure? We are working with two compilers, call them
>A & B, and they pack the record structure inversely. A typical example:
>type sample_message_type is
>   record
>      first_field  : unsigned_20_bits_type;
>      second_field : unsigned_22_bits_type;
>      third_field  : unsigned_22_bits_type;
>   end record
>sample_message : sample_message_type;

You haven't specified what the representation is.  You MUST specify the
representation of all data that leaves an Ada application, and transmitted
to an external device.

The solution is simple: include a representation clause for your type, as in

for Sample_Message_Type use
   record
      First_Field at 0 range 0 .. 19;
      Second_Field at 0 range 20 .. 41;
      Third_Field    at 0 range 42 .. 63;
   end record;

for Sample_Message_type'Size use 64;

Since you didn't specify the representation of the type, the compiler is
free to place the record components where ever it feels like.

Watch out for endian issues.  Are computers the same endianess?  You may
have to do byte-swapping too.

Make sure you get the positions correct.  Ada is consistent in the sense
that a bigger value means a bigger address, but this is contrary to what
you'd expect on a big-endian machine.  For example,

type RT is
   record
      B : Boolean;
   end record;

for RT use
   record
      B at 0 range 0 .. 0;
   end record;

for RT'Size use 8;

On a little endian machine, B is where you expect it: at the least
significant bit, at the position corresponding to the value 2 ** 0.

However, on a big endian machine, the same declaration puts B at the most
significant bit, at the position corresponding to the value 2 ** 7.

This "feature" of Ada can be mitigated by using the Bit_Order attribute
(available only in Ada 95 though), but I don't think any compiler actually
supports it.  I hope I'm wrong.  Even with it, you still have an ...um...
"issue" indexing packed bit arrays.

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




  reply	other threads:[~1998-01-12  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-01-12  0:00 Packing Record Structures in Ada Randall_Rathbun
1998-01-12  0:00 ` Matthew Heaney [this message]
1998-01-13  0:00 ` Robert Dewar
1998-01-13  0:00   ` Corey Minyard
1998-01-13  0:00     ` Robert Dewar
1998-01-15  0:00     ` Michael F Brenner
1998-01-15  0:00       ` Robert Dewar
1998-01-19  0:00 ` Anonymous
replies disabled

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