comp.lang.ada
 help / color / mirror / Atom feed
From: Keith Thompson <kst@cts.com>
Subject: Re: Help: Storage size
Date: 1999/07/14
Date: 1999-07-14T23:38:16+00:00	[thread overview]
Message-ID: <yecaesyajko.fsf@king.cts.com> (raw)
In-Reply-To: 7mfndb$mc8$1@nnrp1.deja.com

Adrian Hoe <byhoe@my-deja.com> writes:
> In article <7mf7lp$c9g@hobbes.crc.com>,
>   "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> wrote:
[...]
> > 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).

In purely C terms, this is a bad idea.  The C standard allows a
bit-field to be of type int, signed int, or unsigned int (in this
context only, it's implementation-defined whether a plain "int"
is signed or unsigned).  Thus an unsigned short bit-field is,
strictly speaking, illegal -- though many (most?) implementations
will accept it as an extension.  (Try compiling your C source with
"gcc -ansi -pedantic -Wall".)  Many other aspects of bit-fields are
either implementation-defined or unspecified.

If you want a C integer type of a specified width, it's usually best
to declare it as a typedef whose definition varies from one system
to another.  C9X, the next revision of the C standard, will provide
such typedefs in the new predefined header <stdint.h>, but you can
easily roll your own.  Part of the job of porting your code to another
architecture is then updating the typedefs.  (Be sure to include some
automated checking code to make sure you got it right.)

Once you've done this, matching the C integer types to corresponding
Ada types is pretty easy.  For example, an Ada 16-bit unsigned type
can be declared as:

    type Unsigned_16 is mod 2**16;
    for Unsigned_16'Size use 16;

The exact layout of record components / struct members is
implementation-defined in both languages.  Using pragma Convention(C)
on the Ada side should guarantee that they match.

The only real drawback of this approach is that not all platforms
actually have a 16-bit integer type.  In the unlikely event that this
becomes an issue (for example, on some Crays), I suppose you can
always fall back to bit-fields.

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
One of the great tragedies of ancient history is that Helen of Troy
lived before the invention of the champagne bottle.




      parent reply	other threads:[~1999-07-14  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-07-13  0:00 Help: Storage size Adrian Hoe
1999-07-13  0:00 ` David C. Hoos, Sr.
1999-07-13  0:00   ` Adrian Hoe
1999-07-13  0:00     ` David C. Hoos, Sr.
1999-07-14  0:00       ` Adrian Hoe
1999-07-14  0:00         ` Simon Wright
1999-07-16  0:00           ` Adrian Hoe
1999-07-16  0:00       ` Adrian Hoe
1999-07-14  0:00     ` Keith Thompson [this message]
replies disabled

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