comp.lang.ada
 help / color / mirror / Atom feed
From: cjrgreen@concentric.net (Christopher Green)
Subject: Re: Alternate to Unchecked_Conversion - Portable?
Date: 1999/02/22
Date: 1999-02-22T00:00:00+00:00	[thread overview]
Message-ID: <36d2638e.6427631@nntp.concentric.net> (raw)
In-Reply-To: 36d05e39.0@news.pacifier.com

On Sun, 21 Feb 1999 11:33:54 -0800, "Steve Doiel"
<nospam_steved@pacifier.com> wrote:

>I discovered a technique for performing a similar function to
>Unchecked_Conversion.  Locating two data structures at the same address by
>defining the first normally and then using an address clause to locate the
>second at the same address as the first (as shown in the following sample):
>
>
>WITH Ada.Text_Io;
>WITH Interfaces;
> USE Interfaces;
>PROCEDURE TestAlias IS
>  PACKAGE Text_Io RENAMES Ada.Text_Io;
>
>  TYPE aRec IS
>    RECORD
>      f1 : Integer_32;
>      f2 : Integer_32;
>    END RECORD;
>
>  TYPE Integer_16_Array IS ARRAY( Positive RANGE <> ) OF Integer_16;
>
>  v1 : aRec;
>  v2 : Integer_16_Array( 1..4 );
>  FOR v2'ADDRESS USE v1'ADDRESS;    -- This causes v1 and v2 to occupy the
>same memory
>
>BEGIN
>  v1.f1 := 1;
>  v1.f2 := 2;
>  FOR ii IN v2'RANGE LOOP
>    Text_Io.Put( Integer_16'IMAGE( v2( ii ) ) );
>  END LOOP;
>  Text_Io.New_Line;
>END TestAlias;
>
>
>My question is: is this portable?
>
>I expect that record layouts will be dependent on different endian machines,
>and I know that this technique is inherently unsafe, but it makes things
>very simple.
>
>SteveD
>
>

It is primarily useful in situations in which alternative
implementations end up causing a bitwise copy.  If
the object to be converted is large, or the conversion
must be done many times, this can be a win.

Other than problems with data representation mis-
matches, a source of significant portability problems
is default initializations. For example, certain compilers
will apply default initializations to arrays of derived types
of System.Address, zeroing out the contents of the
array you were trying to convert.

Unless there are overriding reasons to do otherwise;
for example, a need to preserve backward com-
patibility with Ada 83, or a big performance difference
in a particular implementation, it is safer and usually
more efficient to use packages intended for this
purpose, such as Interfaces.C.Pointers.




  parent reply	other threads:[~1999-02-22  0:00 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-21  0:00 Alternate to Unchecked_Conversion - Portable? Steve Doiel
1999-02-21  0:00 ` Steve Quinlan
1999-02-22  0:00   ` robert_dewar
1999-02-21  0:00 ` Matthew Heaney
1999-02-22  0:00 ` Tom Moran
1999-02-22  0:00 ` Christopher Green [this message]
1999-02-23  0:00   ` Matthew Heaney
1999-02-23  0:00     ` Samuel Mize
1999-02-24  0:00     ` robert_dewar
1999-02-25  0:00       ` Nick Roberts
1999-02-25  0:00         ` robert_dewar
1999-02-26  0:00           ` Nick Roberts
1999-02-26  0:00             ` Matthew Heaney
1999-02-27  0:00               ` Nick Roberts
1999-02-24  0:00   ` robert_dewar
1999-02-23  0:00     ` Christopher Green
1999-02-25  0:00       ` robert_dewar
1999-02-24  0:00         ` Christopher Green
1999-02-25  0:00           ` robert_dewar
1999-02-25  0:00             ` dennison
1999-02-25  0:00               ` Christopher Green
1999-02-26  0:00                 ` robert_dewar
1999-02-26  0:00                   ` Christopher Green
1999-03-01  0:00                     ` Nick Roberts
1999-03-01  0:00                       ` dewar
1999-03-01  0:00                         ` Nick Roberts
1999-02-26  0:00                 ` Tom Moran
1999-02-26  0:00                   ` robert_dewar
1999-02-26  0:00             ` Robert A Duff
1999-02-28  0:00               ` robert_dewar
1999-02-23  0:00     ` Christopher Green
1999-02-25  0:00       ` robert_dewar
1999-02-24  0:00         ` Christopher Green
1999-02-25  0:00           ` robert_dewar
1999-02-26  0:00             ` Dale Stanbrough
1999-02-26  0:00               ` Robert A Duff
1999-02-22  0:00 ` robert_dewar
1999-02-22  0:00   ` Samuel Mize
replies disabled

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