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, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d233b131fc3b7ba7 X-Google-Attributes: gid103376,public From: Adrian Hoe Subject: Re: Help: Storage size Date: 1999/07/14 Message-ID: <7mh556$7ue$1@nnrp1.deja.com>#1/1 X-Deja-AN: 500791554 References: <7mejue$9lk$1@nnrp1.deja.com> <7mf7lp$c9g@hobbes.crc.com> <7mfndb$mc8$1@nnrp1.deja.com> <7mfs92$jch@hobbes.crc.com> X-Http-Proxy: 1.0 x40.deja.com:80 (Squid/1.1.22) for client 202.188.38.171 Organization: Deja.com - Share what you know. Learn what you don't. X-Article-Creation-Date: Wed Jul 14 04:54:01 1999 GMT Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.51 [en] (X11; I; Linux 2.0.33 i486) Date: 1999-07-14T00:00:00+00:00 List-Id: In article <7mfs92$jch@hobbes.crc.com>, "David C. Hoos, Sr." wrote: > > Adrian Hoe wrote in message <7mfndb$mc8$1@nnrp1.deja.com>... > >In article <7mf7lp$c9g@hobbes.crc.com>, > > "David C. Hoos, Sr." wrote: > >> > >> Adrian Hoe wrote in message <7mejue$9lk$1@nnrp1.deja.com>... > >> >If I have a C code like this: > >> > > >> >typedef unsigned short CARD16; > >> > > >> >and > >> > > >> >typedef struct { > >> > CARD8 reqType; > >> > CARD16 length : 16; > >> >} tReq; > >> > > >> >How can the above struct (record) be implemented in Ada? > >> > > >> >Where CARD16 is define as follow in Ada: > >> > > >> >type CARD16 is new X.Unsigned_Short; > >> > > >> > > >> >Can anyone please help? > >> >-- > >> Several problems here -- e.g.: > >> > >> 1. Where is the declaration for CARD8? This will affect the result. > > > >CARD8 is declare as unsigned char; > > > > > > > >> 2. If #pragma pack is used, this will affect the result. > > > >#pragma pack is not used. > > > > > > > >> 3. If unsigned short is 16 bits on your system, the bit-field > >> specification (:16) would appear to be redundant. > > > >Most platforms have 16 bits for unsigned short. The bit-field > >specification (e.g. :16) is used to ensure 16 bits mapping on other > >platform(s). > > > > > > > >> 4. Where is the declaration for X.Unsigned_Short? > > > >In X.ads, it is declare as > > > > subtype unsigned_short is interfaces.c.unsigned_short; > > > > > > > >> 5. Bit-field implementations are compiler-dependent, so from > >> the code you supplied (without any compiler or platform > >> identification) it is impossible to say for sure what is > >> the memory layout of the tReq struct. > > > >I am using GNAT for Linux (PC) and intend to target the code to other > >platforms like Sun and Alpha. > > > > > > > >> If the memory layout of the C struct is known, writing the > >> Ada declaration is trivial. > >> > >> > > > > > >Let's assume the bit-field specification (:16 e.g.) is needed badly, how > >can I implement the C's struct in Ada? > >-- > > If you want to be assured that your types have the correct size, > then I would not recommend basing your types on > Interfaces.C.Unsigned_Short, because that type is dependent on the size > of C's unsigned short type for that platform. > > How about starting from scratch -- e.g.: > > type Card8 is mod 2 ** 8; > for Card8'size use 8; > type Card16 is mod 2 ** 16; > for Card16'size use 16; > > type Treq is record > ReqType : Card8; > Length : Card16; > end record; > > for Treq use record > ReqType at 0 range 0 .. 7; > Length at 2 range 0 .. 15; > end record; > > for Treq'size use 32; > > Note that there is one byte of unocupied space between the > record components, consistent with the way gcc lays out > your struct, in the absence of #pragma pack. > > Hope this helps. > > This method gives the most assurance in plain Ada context. But, what if the intention to interface from Ada to the original (assumed) C's struct? Is this method applicable in this context (Interfacing to C)? -- Adrian BY, Hoe -------------- Sent via Deja.com http://www.deja.com/ Share what you know. Learn what you don't.