comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Adress => Access: types for unchecked conversion have different sizes
Date: Sat, 12 Mar 2011 21:32:08 -0600
Date: 2011-03-12T21:32:08-06:00	[thread overview]
Message-ID: <ilhds0$12k$1@munin.nbi.dk> (raw)
In-Reply-To: op.vr8kbsndz25lew@macpro-eth1.krischik.com

"Martin Krischik" <krischik@users.sourceforge.net> wrote in message 
news:op.vr8kbsndz25lew@macpro-eth1.krischik.com...
...
> The code in question is:
>
> {{{
>          declare
>             subtype Path_String is String (1 .. Filename_Len);
>             type    Path_String_Access is access Path_String;

As someone else noted, this is a pool-specific access type. It should never 
point at anything other than memory created by an allocator. (Janus/Ada 
actually checks that; you'd get Constraint_Error if you tried to dereference 
the result of the code below.) If you need to point it at something else, 
you need to use a general access type (that is, "access all").

>             function Address_To_Access is new
>               Ada.Unchecked_Conversion
>                 (Source => Address,
>                  Target => Path_String_Access);
>
>             Path_Access : constant Path_String_Access :=
>                             Address_To_Access (Filename_Addr);
>
>          begin
>             Last := Filename_Len;
>             Name (1 .. Last) := Path_Access.all;
>          end;
> }}}
...
> And I wonder if some `for Address use` magic would not be more appropriate 
> here.

No, that is a lousy way to handle this sort of problem, as is this code. 
(This appears to be an old Ada 83 design, completely inappropriate in modern 
Ada -- and a lousy idea in Ada 83, too.). Ada has package 
Address_to_Access_Conversions for this sort of problem. (That package also 
has an annoyance: it declares a new access type for each instance, forcing 
some extra conversions if you intend to use a type declared elsewhere.)

Lastly, access to unconstrained is rarely the right thing in such code; it 
might work on some compiler some of the time, but will not work in general. 
(Note that Annex B does not require such things to work for interfacing, and 
using addresses is very similar to interfacing.)

                            Randy.





  parent reply	other threads:[~2011-03-13  3:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-12 15:08 Adress => Access: types for unchecked conversion have different sizes Martin Krischik
2011-03-12 17:00 ` Edward Fish
2011-03-12 17:05 ` Simon Wright
2011-03-13  9:35   ` Martin Krischik
2011-03-13 11:57     ` Simon Wright
2011-03-13 13:03       ` Martin Krischik
2011-03-13  3:32 ` Randy Brukardt [this message]
2011-03-13  9:47   ` Martin Krischik
2011-03-13 23:16     ` Randy Brukardt
2011-03-14  7:26       ` Martin Krischik
2011-03-14 18:40         ` Simon Wright
replies disabled

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