Here is a short example showing two methods for dynamic allocation. My reading of the opinions on
comp.lang.ad is that it's best to avoid using 'access (as it can lead to dangling pointers). But as the second method shows, sometimes you have no choice but to use 'access (it can also be used to pass procedure names as parameters but that's another story).
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;
begin
-- example without using 'access
for i in 10 .. 20 loop
-- create xg on the stack (okay but stack size is limited)
-- xg exists only within this declare block
declare
xg : Array (0..i-1) of Float;
begin
Put (xg'last);
New_line;
end;
end loop;
-- example using 'access
-- essential for very very large arrays
declare
-- could put next three lines after "procedure Foo" above
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);
huge : Integer := 128_000_000;
xg_ptr : vector_ptr := new vector (0..huge);
xg : vector renames xg_ptr.all;
begin
Put (xg'last);
New_line;
Free_Vector (xg_ptr); -- essential to avoid memory leaks
end;
end Foo;