-- Collection of code snippets by Arne Vajhøj 
-- (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
with Ada.Text_IO,Ada.Integer_Text_IO,Unchecked_Deallocation;

use Ada.Text_IO,Ada.Integer_Text_IO;

-- pointers & dynamic allocation

procedure Dyn_Mem is

type List_Type;
type List_Type_Access is access List_Type;
type List_Type is
    record
        Id : Integer;
        Name : String(1..10);
        Next : List_Type_Access;
    end record;

procedure Free is new Unchecked_Deallocation(List_Type, List_Type_Access);

First,Curr,Prev : List_Type_Access;
I : Integer;

begin
    First := new List_Type;
    I := 1;
    Curr := First;
    loop
        Curr.all.Id := I;
        Curr.all.Name := "XXXXXXXXXX";
        I := I + 1;
        exit when I > 10;
        Curr.all.Next := new List_Type;
        Curr := Curr.all.Next;
    end loop;
    Curr := First;
    for J in 1..10 loop
        Put(Curr.all.Id,Width=>1);
        Put(" ");
        Put(Curr.all.Name);
        New_line;
        Curr := Curr.all.Next;
    end loop;
    Curr := First;
    for J in 1..10 loop
        Prev := Curr;
        Curr := Curr.all.Next;
        Free(Prev);
    end loop;
end Dyn_Mem;
