comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Rules for Representation of Subtypes
Date: 1996/09/24
Date: 1996-09-24T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023080002409960726040001@news.ni.net> (raw)
In-Reply-To: Dy78qJ.MDr@world.std.com


In article <Dy78qJ.MDr@world.std.com>, bobduff@world.std.com (Robert A
Duff) wrote:

>>I've always been curious about the rules for representation of subtypes. 
>>For example, if I make a declaration like this:
>>
>>   A_Small_Integer : Integer range 0 .. 255;  -- assume type Integer is 4
bytes
>>
>>then is an implementation allowed to use fewer than 4 bytes to represent
>>A_Small_Integer?
>
>Yes.
>
>>Suppose I'm on a Unix system and I do this
>>
>>   read (fd, A_Small_Integer'Address, 4);
>>
>>If the representation of A_Small_Integer is only 1 byte, then obviously I'm
>>in trouble.  Am I required to specify the size of objects?
>
>Well, you could.  But a better solution is to always say "aliased" when
>you have a variable you want to take 'Address of.  Aliased variables
>will generally be represented the same way, for all variables of the
>type.

You say "generally" be represented the same way: can you be more specific? 
Under what circumstances can they be different?

>Also, the number 4 is questionable.  What are you trying to do?  Read 4
>bytes of data?  Well in that case, you want to make sure the target
>variable is 4 bytes (which your code doesn't do).  Or are you trying to
>read however-many-bytes it takes to fill up A_Small_Integer?  In that
>case, you should be asking for that number of bytes, not 4 bytes.  (You
>can calculate it based on 'Size, which, unfortunately, is in bits.)

Perhaps this was a poor example: I would never actually do it that way in
real code.  I just needed an example to ask my question about size of
objects of subtypes. 

Better is:

   A_Small_Integer: Interfaces.Integer_32 range 0 .. 255;

But of course, if I'm going to read 4 bytes from an OS call, then

   The_Value : Interfaces.Integer_32;

is best.


BTW: Why doesn't Ada have a 'Storage_Size clause for (non-access) types or
objects?  If everywhere I want storage units, I have to do this:

   read (fd, O'Address, O'Size / System.Storage_Unit);

If "number of storage units" is required so much, and the expression
O'Size/System.Storage_Unit is so popular, then why didn't the Ada designers
save me the trouble, so I could say

   read (fd, O'Address, O'Storage_Units);

or even

   read (fd, O'Address, O'Number_Of_Bytes);

I've seen this kind of error often, where the programmer sends the 'Size
without dividing by 8 first.  The error is exacerbated when documentation
(say, on a Unix box) refers to size, and means "number of bytes."  So easy
to forget to divide O'Size by 8!

Byte-size is an undeniably popular unit of measure in programming.  The
fact that there's no *direct* support for it in Ada seems to me to be an
egregious oversight.  (But I'm sure there was a reason...)


Here's another question:

   type T is new Interfaces.Integer_32 range 0 .. 255;

What is the size of T?  Do I need to specify a size clause to ensure that
objects of type T are 4 bytes?

>>   A_Small_Integer : Integer range 0 .. 255;
>>   for A_Small_Integer'Size use 32;                     -- required?
>
>That will make it the right size.  But if you read 4 non-zero bytes,
>your program will be erroneous.

You are of course correct.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




  reply	other threads:[~1996-09-24  0:00 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-09-22  0:00 Rules for Representation of Subtypes Matthew Heaney
1996-09-23  0:00 ` Robert A Duff
1996-09-24  0:00   ` Matthew Heaney [this message]
1996-09-26  0:00     ` Robert A Duff
1996-09-26  0:00       ` Larry Kilgallen
1996-09-27  0:00         ` Robert A Duff
1996-09-27  0:00           ` Mark A Biggar
1996-09-30  0:00             ` Robert A Duff
1996-10-01  0:00               ` Larry Kilgallen
1996-10-01  0:00                 ` Robert A Duff
1996-10-01  0:00                 ` Samuel Tardieu
1996-10-01  0:00                   ` Ken Garlington
1996-10-01  0:00                   ` Valid Attribute and Unchecked Conversion Matthew Heaney
1996-10-02  0:00                     ` Robert A Duff
1996-10-04  0:00                       ` Keith Thompson
1996-10-04  0:00                         ` Robert A Duff
1996-10-04  0:00                           ` Robert Dewar
1996-10-11  0:00                             ` Norman H. Cohen
1996-10-12  0:00                               ` Robert Dewar
1996-10-06  0:00                           ` Keith Thompson
1996-10-07  0:00                             ` Robert Dewar
1996-10-09  0:00                               ` Keith Thompson
1996-10-07  0:00                           ` Ken Garlington
1996-10-08  0:00                           ` Alan Brain
1996-10-04  0:00                         ` Matthew Heaney
1996-10-07  0:00                           ` Robert Dewar
1996-10-09  0:00                             ` Keith Thompson
1996-10-07  0:00                         ` Robert Dewar
1996-10-10  0:00                           ` Ken Garlington
1996-10-11  0:00                             ` Robert Dewar
1996-10-14  0:00                               ` Keith Thompson
1996-10-14  0:00                               ` Ken Garlington
1996-10-07  0:00                         ` Kenneth Almquist
1996-09-28  0:00           ` Rules for Representation of Subtypes Larry Kilgallen
1996-09-29  0:00             ` Robert A Duff
1996-09-29  0:00               ` Larry Kilgallen
1996-09-29  0:00                 ` Matthew Heaney
1996-09-30  0:00                 ` Robert A Duff
1996-10-01  0:00                   ` Ken Garlington
1996-10-02  0:00                     ` Robert A Duff
1996-10-02  0:00                       ` Ken Garlington
1996-10-06  0:00                   ` Robert Dewar
1996-09-29  0:00               ` Matthew Heaney
1996-09-30  0:00                 ` Robert A Duff
1996-09-30  0:00                 ` Robert Dewar
1996-09-30  0:00                   ` Matthew Heaney
1996-10-03  0:00             ` Robert Dewar
1996-10-02  0:00         ` Valid Attribute and Unchecked Conversion Robert I. Eachus
1996-10-02  0:00           ` Matthew Heaney
1996-10-02  0:00         ` George Haddad
1996-10-03  0:00           ` John Herro
1996-10-04  0:00             ` Karl Cooper {46901}
1996-10-07  0:00               ` Mark A Biggar
1996-10-08  0:00                 ` Robert Dewar
1996-10-05  0:00             ` Robert Dewar
1996-10-06  0:00               ` Keith Thompson
1996-10-14  0:00                 ` Robert A Duff
1996-09-27  0:00       ` Rules for Representation of Subtypes Matthew Heaney
1996-09-27  0:00         ` Robert A Duff
1996-09-23  0:00 ` David C. Hoos, Sr.
1996-09-23  0:00   ` Samuel T. Harris
1996-09-26  0:00     ` David C. Hoos, Sr.
1996-09-23  0:00   ` Robert A Duff
1996-09-24  0:00   ` Robert Dewar
1996-09-24  0:00   ` Robert Dewar
1996-09-26  0:00     ` Keith Thompson
1996-09-26  0:00       ` Matthew Heaney
1996-09-27  0:00         ` Robert Dewar
1996-09-27  0:00         ` Robert A Duff
1996-09-27  0:00           ` Robert Dewar
1996-09-27  0:00       ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
1996-09-28  0:00 Robert Dewar
1996-09-29  0:00 ` Robert A Duff
1996-09-29  0:00   ` Robert Dewar
1996-09-30  0:00     ` Art Schwarz
1996-09-30  0:00       ` Robert A Duff
1996-10-01  0:00       ` Larry Kilgallen
1996-10-01  0:00         ` Robert A Duff
1996-10-01  0:00         ` Brian R. Hanson
1996-09-29  0:00   ` Matthew Heaney
1996-09-28  0:00 Robert Dewar
1996-09-30  0:00 ` Keith Thompson
1996-09-28  0:00 Robert Dewar
1996-09-29  0:00 ` Robert A Duff
1996-09-29  0:00   ` Matthew Heaney
1996-10-02  0:00 Franco Mazzanti
1996-10-03  0:00 ` Robert A Duff
1996-10-03  0:00 Franco Mazzanti
1996-10-03  0:00 ` Robert A Duff
1996-10-10  0:00 W. Wesley Groleau (Wes)
1996-10-10  0:00 ` Robert Dewar
1996-10-11  0:00 ` Ken Garlington
replies disabled

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