Skip to content

Instantly share code, notes, and snippets.

@mk270
Created February 16, 2016 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mk270/ed606797cb8203fdbaf7 to your computer and use it in GitHub Desktop.
Save mk270/ed606797cb8203fdbaf7 to your computer and use it in GitHub Desktop.
practice returning a dynamically allocated array
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line;
with Ada.Unchecked_Deallocation;
package body Dynamic_Allocation_Experiment
is
type Side is (Left, Right);
type Side_Array is array (Integer range <>) of Side;
type Side_Array_Ptr is access Side_Array;
procedure Free_Side_Array is
new Ada.Unchecked_Deallocation(Side_Array, Side_Array_Ptr);
function Make_Sides (Size : Natural) return Side_Array_Ptr
is
begin
declare
Sides_Ptr : Side_Array_Ptr := new Side_Array (1 .. Size);
Toggle : Boolean := True;
begin
for I in Sides_Ptr'Range loop
Sides_Ptr (I) := (if Toggle then Right else Left);
Toggle := not Toggle;
end loop;
return Sides_Ptr;
end;
end Make_Sides;
procedure Dump_Sides (S : Side_Array_Ptr) is
begin
for I in S'Range loop
Ada.Text_IO.Put_Line (Side'Image (S (I)));
end loop;
end Dump_Sides;
procedure Dynamic_Allocation_Experiment
is
S : Side_Array_Ptr;
Arg1 : String := Ada.Command_Line.Argument (1);
Size : Integer := Integer'Value (Arg1);
begin
S := Make_Sides (Size);
Dump_Sides (S);
Ada.Text_IO.Put_Line ("len: " & Integer'Image (S'Length));
Free_Side_Array (S);
end Dynamic_Allocation_Experiment;
end Dynamic_Allocation_Experiment;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment