-- Shows exhausting the heap
with Text_IO; use Text_IO;
with System.Address_Image;
with Ada.Exceptions; use Ada.Exceptions;

procedure exhheap is

   count : Natural := 0;

   type bigarray is array (1 .. 10_000) of integer;
   type bap is access bigarray;

   bp : bap := new bigarray;
begin
   put_line("Address of bp: " & System.Address_Image(bp.all'address));
   loop
      bp := new bigarray;
      count := count + 1;
   end loop;

exception
      when e: others => 
         put_line (exception_name (e) & ": " & exception_message (e));
         put_line ("Count: " & count'img);
         put_line("Address of bp: " &
         System.Address_Image(bp.all'address));
end exhheap;

--  Address of bp: 0807F008
--  STORAGE_ERROR: heap exhausted
--  Count:  80377
--  Address of bp: BFF57310