-- Illustrates the following:
-- deallocation with dispose
-- dispose null
--
-- Make this generic procedure available
with Unchecked_Deallocation;
with ada.text_io; use ada.text_io;
procedure boxdispose is
type Box is record
l, w: Integer := 0;
end record;
type BoxPtr is access Box;
procedure dispose is new Unchecked_Deallocation(
Object => Box, -- The type of object being deallocated
Name => BoxPtr -- The type of pointer being followed
);
p: BoxPtr;
begin
p := new Box'(1, 2);
-- Code that uses the Box goes here
-- Now we're done with the first Box
-- Deallocate the first Box
dispose(p);
-- Now we can allocate another Box. No garbage created!
p := new Box'(3, 4);
-- Deallocation sets the pointer to null
dispose(p);
if p = null then put_line("It's null!"); end if;
-- Disposing null is okay
dispose(p);
end boxdispose;