Re: Pascal program file parameter to Ada translation (Was: Re: Is GPC dead?)
Leo Brewin <[email protected]> Thu, 5 Jan 2017 10:32:15 +1100
| Newsgroups | gmane.comp.compilers.gpc |
|---|---|
| Message-ID | <CAP-nitJAzOyYTtODJN9m7PA0X355=mMMxTjjnF8-UKy-7NOspg@mail.gmail.com> |
Hi Kevan & Bastiaan,
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).
Cheers,
Leo
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;
On 5 January 2017 at 09:38, Bastiaan Veelo <[email protected]> wrote:
>
> > On 4 Jan 2017, at 23:26, Kevan Hashemi <[email protected]> wrote:
> >
> > My main concern is whether I can implement the following dynamic
> allocation of memory space for graphs, images, and matrices.
> >
> > type
> > graph_type(num_points:integer)=array [0..num_points-1] of real;
> > graph_ptr=^graph_type;
> > var
> > xg:graph_ptr;
> > i:integer;
> > begin
> > for i:=100 to 200 do begin
> > new(xg,i);
> > dispose(xg,i);
> > end;
>
> I just came across an Ada example that may help you:
> https://en.wikibooks.org/wiki/Ada_Programming/Types/access#
> Deleting_objects_from_a_storage_pool
>
> I am pleased to see this discussion, because at my work we are somewhat in
> the same boat. We use the Prospero compiler, which finally appears to have
> vanished from the Internet. We have previously considered gpc, but its
> limited support for Extended Pascal has stopped us. We will have a serious
> look at Ada.
>
> Bastiaan.
>
_______________________________________________
Gpc mailing list
[email protected]
https://www.g-n-u.de/mailman/listinfo/gpc