Hi Kevan,
> huge : Integer := 128_000_000;
> Here the size of the array is predefined with a constant "huge".
No. "huge" is a variable that is initialised to 128,000,000. The memory is allocated dynamicly and it is allocated from the heap (the main memory pool).
Perhaps this is a better example for what you want
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Deallocation;
procedure Foo is
package Integer_IO is new Ada.Text_IO.Integer_IO (Integer); use Integer_IO;
type vector is array (Integer range <>) of Float;
type vector_ptr is access vector;
procedure Free_Vector is new Ada.Unchecked_Deallocation (vector, vector_ptr);
num : Integer;
begin
get (num); -- read the size of the array from standard input
declare
xg_ptr : vector_ptr := new vector (0..num);
xg : vector renames xg_ptr.all;
begin
Put (xg'last);
New_line;
Free_Vector (xg_ptr); -- essential to avoid memory leaks
end;
end Foo;
And another example,
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Deallocation;
procedure Foo is
package Integer_IO is new Ada.Text_IO.Integer_IO (Integer); use Integer_IO;
type vector is array (Integer range <>) of Float;
type vector_ptr is access vector;
procedure Free_Vector is new Ada.Unchecked_Deallocation (vector, vector_ptr);
min, max : Integer;
procedure bahbah (min,max : Integer) is
xg_ptr : vector_ptr := new vector (min..max); -- min, max unknown at compile time
xg : vector renames xg_ptr.all;
begin
Put (xg'last);
New_line;
Free_Vector (xg_ptr); -- essential to avoid memory leaks
end bahbah;
begin
get (min);
get (max);
bahbah (min,max);
end Foo;
Cheers,
Leo