-- Shows stack some addresses with infinite recursion
with Text_IO; use Text_IO;
with System.Address_Image;
with Ada.Exceptions; use Ada.Exceptions;
procedure infrec is
   type Unsigned_32 is mod 2 ** 32;
   prev_addr : Unsigned_32;

   count : Natural := 0;
   procedure A is
      ai: Integer;   -- Unitialized
      int_address : Unsigned_32 :=
         Unsigned_32'Value ("16#" & System.Address_Image(ai'address) & "#");
   begin
      if count <= 3 then 
         put_line("Address of ai: " & System.Address_Image(ai'address)
               & " = " & int_address'img);
      end if;
      count := count + 1;
      prev_addr :=
         Unsigned_32'Value ("16#" & System.Address_Image(ai'address) & "#");
      a;
   exception
      when e: others => 
         new_line;
         put_line (exception_name (e) & ": " & exception_message (e));
         put_line ("Count: " & count'img);
         int_address :=
            Unsigned_32'Value ("16#" & System.Address_Image(ai'address) & "#");

         put_line("Address of ai: " & System.Address_Image(ai'address)
               & " = " & prev_addr'img);
   end A; 

   i: Integer;      -- Address of i: BF98044C

begin

   put_line("Address of i: " & System.Address_Image(i'address));
   new_line;

   A;               -- ai and Address of ai: -1080556592: BF980380

exception
   when e: others => 
      new_line;
      put_line ("ERROR: " & exception_name (e) & ": " & exception_message (e));
end infrec;
--  Address of i: BF82D658
--  
--  Address of ai: BF82D2D0 =  3213021904
--  Address of ai: BF82CE00 =  3213020672
--  Address of ai: BF82C930 =  3213019440
--  Address of ai: BF82C460 =  3213018208
--  
--  STORAGE_ERROR: stack overflow or erroneous memory access
--  Count:  6806
--  Address of ai: BF02E5C0 =  3204638144
--  
--  STORAGE_ERROR: stack overflow or erroneous memory access
--  Count:  6806
--  Address of ai: BF02EA90 =  3204638144