comp.lang.ada
 help / color / mirror / Atom feed
From: Keith Thompson <kst@cts.com>
Subject: Re: A Record Interpretation of an Array
Date: 30 May 2001 17:55:57 -0700
Date: 2001-05-30T17:55:57-07:00	[thread overview]
Message-ID: <yecsnhmfiaq.fsf@king.cts.com> (raw)
In-Reply-To: 3B12C9E0.5E9B06C8@earthlink.net

"Marc A. Criley" <mcqada@earthlink.net> writes:
> I'll start with disclaimers :-)
> 
> I know what I'm going to be asking about is not defined by the language,
> is non-portable, is implementation defined, etc.  I'm just looking to
> gather some thoughts and opinions here.
> 
> Consider a plain, simple, fixed-length array of some type--no variants,
> tagged types, unconstrained anythings, packing pragmas or
> specifications.  Just your plain 'ole basic array.  For example, an
> array (1..5) of Integer.
> 
> If I wanted to access the contents of this array as a analogous record,
> I could declare something like:
> 
>   type Record_View is
>     record
>       Field1 : Integer;
>       Field2 : Integer;
>       Field3 : Integer;
>       Field4 : Integer;
>       Field5 : Integer;
>     end record;
> 
> and do an Unchecked_Conversion between the array type and this record
> type.
> 
> Is it reasonable to think this conversion (going either way) would give
> sensible results?  Is there a stronger basis for that answer than a
> naive "reasonable to think"?

This will often work, but it's not guaranteed.  In fact, I know of an
Ada 83 implementation (some versions of TeleSoft's TeleGen2/RISCAda)
that would reorder record components, even if they're all of the same
type.  It normally reordered record components to minimize gaps; for
example, it would try to put components with stricter alignment
requirements first.  In the initial implementation, it used an
unstable sort (one that would not maintain the original ordering for
elements with equal keys -- I think it was a straight Quicksort), so
your Record_View might be ordered something like Field3, Field5,
Field2, Field4, Field1.

It's quite possible that no current implementation does this.  If you
decide to depend on that, I suggest that the first thing your code
should do is test whether things are laid out as you expect them to
be, and raise a fatal exception if they aren't.

You might have a better chance of getting away with it if you use
something like "pragma Convention(C, Record_View);".

Of course the most reliable way is to completely specify the layout
yourself, but then you're overriding the compiler's knowledge of what
layout is most efficient.

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.



  parent reply	other threads:[~2001-05-31  0:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-28 22:53 A Record Interpretation of an Array Marc A. Criley
2001-05-29  3:29 ` Jeffrey Carter
2001-05-29 13:34 ` Ted Dennison
2001-05-29 14:16 ` Marin David Condic
2001-05-31  0:55 ` Keith Thompson [this message]
2001-05-31 12:47   ` Marc A. Criley
2001-06-01  1:22     ` Keith Thompson
2001-06-01  0:14 ` Mark
2001-06-01  8:45   ` Ehud Lamm
2001-06-01 12:39   ` Marc A. Criley
2001-06-01 18:20     ` Tucker Taft
replies disabled

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