From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9ed448b3da4e578c X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Interfacing Ada to C Date: 1997/05/30 Message-ID: #1/1 X-Deja-AN: 245145907 References: <5mjvk9$sm7$1@luna.ffi.no> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-05-30T00:00:00+00:00 List-Id: In article , Samuel Tardieu wrote: > type My_Natural is range 0 .. 15; > for My_Natural'Size use 4; > > type R is record > A, B : My_Natural; > end record; > pragma Pack (R); > for R'Size use 8; > >If you do: > > My_R : aliased constant R := (1, 2); > >then My_R will be represented in memory by a single byte containing >00010010 (or 00100001, depending on the endianness). I'm not sure that the statement "depending on the endianness" is correct. Isn't the compiler free to put A and B wherever it chooses? You have no guarantee that A comes later or earlier than B, no matter what the endianess is. All pragma Pack does is minimize the gaps between the components; it has nothing to say about the location of the components within the enclosing record (I think). Which is why I wouldn't recommend it for interfacing to another language. A record representation clause should be used to explictly state the location of each and every component, so it's absolutely unambiguous what the layout is. for R use record A at 0 range 0 .. 3; B at 0 range 4 .. 7; end record; for R'Size use 8; for R'Bit_Order use Low_Bit_First; This puts A on the right (least significant) end of the record, no matter what the endianess is. I don't use pragma Pack except on an array of Booleans, because the only time that pragma Pack makes a guarantee about representation is on an array whose component size is 1. Sadly, there's no equivalent to Bit_Order for arrays. What I would like to be able to do is specify that the first bit (0) occupies the least significant location of the storage element, ie type Bit_Array is array (Natural range 0 .. 7) of Boolean; pragma Pack (Bit_Array); type Byte is range 0 .. 255; for Byte'Size use 8; function To_Byte is new Unchecked_Conversion (Bit_Array, Byte); Bits : constant Bit_Array := (0 => True, others => False); Bits_As_Byte : constant Byte := To_Byte (Bits); On a little endian machine, Bits_As_Byte has the value 1. On a big endian machine, Bits_As_Byte has the value 128. It would be cool if there were an attribute analogous to Bit_Order for arrays: for Bit_Array'Index_Order use Low_Order_First; That way bit 0 would occupy the least significant bit. Oh well, maybe Ada 0X. -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271