with Unchecked_Deallocation;
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
procedure danglingref is
type Box is record
l, w: Integer := 0;
end record;
type BoxPtr is access Box;
procedure dispose is new Unchecked_Deallocation(Box, BoxPtr);
procedure put_line(aBoxPtr: BoxPtr) is
begin
put(aBoxPtr.all.l); put(aBoxPtr.all.w); new_line;
end put_line;
p, q: BoxPtr;
begin
p := new Box'(1, 2);
put_line(p); -- output: 1 2
q := p; -- Create an alias
dispose(q);
put_line(p); -- output unpredictable (example: 0 2)
q := new Box'(3, 4);
-- p may be an alias to the new object
put_line(p); -- may output 3 4
p.all.w := 99;
put_line(q); -- may output 3 99
end danglingref;
-- Typical Output:
-- 1 2
-- 0 2
-- 3 4
-- 3 99