comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: [Q]: Unchecked conversion
Date: 1996/11/10
Date: 1996-11-10T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023280001011961522420001@news.ni.net> (raw)
In-Reply-To: 01bbcf21$4b32a020$829d6482@joy.ericsson.se


In article <01bbcf21$4b32a020$829d6482@joy.ericsson.se>, "Jonas Nygren"
<ehsjony@ehs.ericsson.se> wrote:


>I'm not sure if its portable, but I have used something similar to
>
>    function To_Buffer (S : String) return Buffer is
>       Temp_Buffer : Buffer(1..S'Length)
>       for Temp_Buffer use at S'Address;
>    begin
>       return Temp_Buffer;
>    end To_Buffer;

In general, you should avoid overlay-style conversions when
Unchecked_Conversion will do.  Overlays are inherently dangerous because
you can unwittingly over-write memory that doesn't belong to you,
especially when the overlay-object has default initialization (for example,
an access object).

The preferred approach is as Bob Dewar suggested:

function To_Buffer (S : String) return Buffer is

   subtype Constrained_String is String (S'Range);
   subtype Constrained_Buffer is Buffer (S'Range);

   function To_Constrained_Buffer is 
      new Unchecked_Conversion (Constrained_String, Constrained_Buffer);
begin
   return To_Constrained_Buffer (S);
end;

This should be portable across all Ada compilers, because the source and
target types are both constrained.

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




  parent reply	other threads:[~1996-11-10  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-10  0:00 [Q]: Unchecked conversion Jonas Nygren
1996-11-10  0:00 ` Robert Dewar
1996-11-10  0:00 ` Matthew Heaney [this message]
1996-11-10  0:00   ` Robert Dewar
1996-11-11  0:00     ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
1996-11-09  0:00 [Q] : " Jonas Nygren
1996-11-09  0:00 ` Robert Dewar
1996-11-12  0:00 ` Stephen Leake
replies disabled

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