comp.lang.ada
 help / color / mirror / Atom feed
From: Mark Johnson <Mark_H_Johnson@Raytheon.com>
Subject: Re: Confused about Attribute Size?
Date: Tue, 22 Jan 2002 16:56:09 -0600
Date: 2002-01-22T16:56:09-06:00	[thread overview]
Message-ID: <3C4DEE09.B34A579@Raytheon.com> (raw)
In-Reply-To: a2j3lh$5nt$1@newstoo.ericsson.se

Zebylon wrote:
> 
> Hi,
> 
> I'm a newbie in Ada but I saw an Ada example that confused me a bit, here it
> is:
> 
See comments after each block of code and the questions at the end....

> type My_Type is
>     (startbit,
>      endbit,
>      timeout,
>      spare,
>      nextbit,
>      ......
>      ......
>      lastbit);
> 
Ok, we define an enumeration of several values.

> for My_Type use
>     (startbit =>0,
>      endbit=>1,
>      timeout=>2,
>      spare=>3,
>      nextbit=>4,
>      ......
>      ......
>      lastbit=>15);
> 
A representation specification that requires startbit to be zero, endbit
to be one, and so on. Assuming the items are sequential, there are 16
possible values, 0 through 15.

> for My_Type'Size use 8;
> 
Another representation specification. In this case, you are asking that
the 16 possible values to be stored in an eight bit byte (or larger).
There are several cases where the compiler can use more than 8 bits to
store My_Type...
 - a record component if the record is NOT packed
 - array components
 - a formal parameter passed to a function or procedure
There are several pages of dense text in the Annotated Ada Reference
Manual in section 13.3 that describes this in far more detail that I
will attempt to describe - especially to a newbie.

> type My_Type_Array is array (My_Type) of Boolean;
> 
An array of 16 boolean values (index is startbit..lastbit). Hmm. Since
you did not specify a representation specification nor pragma Packed for
this data type, the compiler could...
 - generate a 16 bit array (two bytes)
 - generate a 16 byte array (16 bytes)
 - generate a 16 [some easy to address size goes here] array (16 x n
bytes)
Without the representation specification or pragma, "you don't care"
about the size, you care about an efficient implementation.

> What I don't understand is how you can assign the size to 8 bits?
> How will this work when there are 16 bits in the type?
I assume you refer to My_Type'Size (8) and not My_Type_Array'Size
(>=16). Eight bits is more than enough bits to represent 16 values from
0 to 15.

> What will be the size of the array?
Hmm. You didn't specify the size explicitly, therefore the compiler can
pick something "convenient". As I mentioned above, it will likely be
some multiple of 16 bits. As small as 16, not likely, but could as large
or larger than 16 x 64.

> This example was in Ada 83 could this be a clue?
Nope. At this level, this code would be the same in Ada 83 and Ada 95.

--
  --Mark



  parent reply	other threads:[~2002-01-22 22:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-22  7:39 Confused about Attribute Size? Zebylon
2002-01-22 19:08 ` Patrick Hohmeyer
2002-01-22 22:56 ` Mark Johnson [this message]
2002-01-23  2:20   ` Jeffrey Carter
2002-01-23 14:56     ` Mark Johnson
2002-01-23 17:06       ` Jeffrey Carter
2002-01-24  4:11         ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
2002-01-22  8:00 Christoph Grein
replies disabled

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