Rules for all pointers
MC logo
 

Rules for all pointers

Ada Code Examples


<<Skip and Read 3 Download Function Access>>
--
-- Rules for all-type pointers.
--
with Gnat.Io; use Gnat.Io;
procedure Allptr is

   type Acc_Int is access all Integer;  -- Type to point to integers.
   WheresFred: Acc_Int;                 -- Such a variable.

   Fred: aliased Integer;               -- An integer which can be pointed to.
   Barney: Integer;                     -- Integer which cannot be pointed to.

   procedure Proceed is
      type Re_Acc_Int is access all Integer;
      Re_Fred: aliased Integer := 389;  -- An integer which can be pointed to.
      My_Fred_Loc: Re_Acc_Int;          -- Something that can point to it.
      Fred_Loc: Acc_Int;                -- This is the type above.
   begin
      My_Fred_Loc := Re_Fred'Access;    -- Change local fred.
      My_Fred_Loc.all := 221;

      My_Fred_Loc := Fred'Access;       -- Change global fred using local
      My_Fred_Loc.all := 22;            --  pointer.

      Fred_Loc := WheresFred;           -- Change global fred again using
      Fred_Loc.all := Fred_Loc.all + 17;--  local pointer of global type.

      Put(Fred);
      Put(" ");
      Put(Re_Fred);
      New_Line;

      -- WheresFred := Re_Fred'Access;  -- Illegal because WheresFred has a
                                        --   longer life than Re_Fred.
      -- My_Fred_Loc := WheresFred;     -- Illegal because types differ.
      -- WheresFred := My_Fred_Loc;     -- Ditto.
      -- Fred_Loc := Re_Fred'Access;    -- Illegal because the type of Fred_Loc
                                        --   has a longer life than Re_Fred.
      WheresFred := Fred_Loc;           -- This is allowed because the fact
                                        --   that things like the last one are
                                        --   illegal means Fred_Loc cannot
                                        --   contain anything WheresFred should
                                        --   not have.
   end Proceed;

begin
   -- Make WheresFred point to Fred, then change Fred.
   WheresFred := Fred'Access;
   WheresFred.all := 99;

   -- WheresFred := Barney'Access;      -- Illegal because Barney was not
                                        --   declared aliased.
   Proceed;

   Put(Fred);
   New_Line;
end Allptr;
<<Skip and Read 3 Function Access>>