comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Not null feature with anonymous and named access types
Date: Fri, 16 Jun 2006 20:16:33 -0500
Date: 2006-06-16T20:16:33-05:00	[thread overview]
Message-ID: <bvOdncD78JBfyg7ZnZ2dnUVZ_qydnZ2d@megapath.net> (raw)
In-Reply-To: 1150144396.104055.164310@f6g2000cwb.googlegroups.com

"Anh Vo" <anhvofrcaus@gmail.com> wrote in message
news:1150144396.104055.164310@f6g2000cwb.googlegroups.com...
> I have been exploring the not null feature with anonymous access type
> and named access type. One thing have learned that an access variable
> declared based on these types will raise a Constraint_Error when
> deallocating this access variable as shown in the code below.
>
> Access_Type:
> declare
>    type Access_Integer is not null access all Integer;

It's virtually always wrong to declare a *type* with "not null". It's use
should primarily be restricted to subtypes and parameters.

>    procedure Free is new Unchecked_Deallocation (Integer,
> Access_Integer);

This instantiation is illegal; the "not null" property must match between
the formal and actual types. See 12.5.4(4/2).

>    My_Ref_1 : Access_Integer := new Integer' (111);
>    My_Ref_2 : not null access Integer := new Integer' (222);
> begin
>    -- perform action on My_Ref_1 and My_Ref_2
>    Free (My_Ref_1);  -- raising Constraint_Error under GNAT/gcc-4.2.0

You shouldn't have been able to run this program.

>    Free (Access_Integer (My_Ref_2)); -- did too
> end Access_Type;
>
> Does this behavior reflect ARM 2005 requirements?

No, see above.

> Thanks in advance for your comments.

As I said above, never declare a null-excluding type; it's impossible to use
in any meaningful way. Use "not null" in subtypes, object declarations,
parameters, and the like. (Parameters are expected to be the primary use.)

I'd write your example like:

Access_Type:
 declare
   type Access_Integer is access all Integer;

    procedure Free is new Unchecked_Deallocation (Integer, Access_Integer);

    My_Ref_1 : Access_Integer := new Integer' (111);
    My_Ref_2 : not null access Integer := new Integer' (222);
begin
    -- perform action on My_Ref_1 and My_Ref_2
    Free (My_Ref_1);  -- OK.
    --Free (Access_Integer (My_Ref_2)); -- Raises Constraint_Error.
    My_Ref_1 := My_Ref_2;
    Free (My_Ref_1);  -- OK.
end Access_Type;

(But the important use of "not null" is in passing parameters.)

                     Randy.





  parent reply	other threads:[~2006-06-17  1:16 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-12 20:33 Not null feature with anonymous and named access types Anh Vo
2006-06-12 21:26 ` Björn Persson
2006-06-12 23:13   ` Anh Vo
2006-06-13  7:53     ` Dmitry A. Kazakov
2006-06-13 15:27       ` Anh Vo
2006-06-14 15:13         ` Alex R. Mosteo
2006-06-14 15:37           ` Anh Vo
2006-06-14 17:00             ` Dmitry A. Kazakov
2006-06-15  3:48               ` Anh Vo
2006-06-15  8:21                 ` Dmitry A. Kazakov
2006-06-17  1:21                   ` Randy Brukardt
2006-06-17  8:24                     ` Dmitry A. Kazakov
2006-06-17 14:24                     ` Robert A Duff
2006-06-19 23:17                       ` Randy Brukardt
2006-06-15 10:50                 ` Alex R. Mosteo
2006-06-17  1:16 ` Randy Brukardt [this message]
2006-06-17 14:25   ` Robert A Duff
replies disabled

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