Re: Pascal program file parameter to Ada translation (Was: Re: Is GPC dead?)
Leo Brewin <[email protected]> Fri, 6 Jan 2017 09:36:40 +1100
| Newsgroups | gmane.comp.compilers.gpc |
|---|---|
| Message-ID | <CAP-nitKiVx=WuBy6_ytW4vRXD7cY3ggCz0surEH1e+=X7uB_BA@mail.gmail.com> |
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
On 6 January 2017 at 04:39, Kevan Hashemi <[email protected]> wrote:
> Leo: Thank you for your code example.
>
> 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;
>>
>
> That's the dynamic allocation I'm looking for, except it's on the stack. I
> need to use the main memory for dynamic allocation.
>
> 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;
>>
>
> Here the size of the array is predefined with a constant "huge".
>
> Bastiaan: Thanks for the link. In the linked code we have:
>
> > VA := new Vector (1 .. 10);
> > VB := VA; -- points to the same location as VA
>
> Here, the vector size is predefined with the constants 1 and 10.
>
> Peter: Thank you for your FPC example, which does exactly what I need.
>
> > program A;
> > type
> > graph_type = array of real;
> > var
> > xg:graph_type;
> > i:integer;
> > begin
> > for i:=100 to 200 do
> > begin
> > setlength(xg,i);
> > xg := nil;
> > end;
> > end.
>
> Somehow, I missed setlength when I looked into FPC, and when I asked the
> FPC group about dynamic arrays, they must have interpreted my question as
> "does FPC support exactly the same syntax as GPC". So it looks like I could
> switch to FPC with minimal work.
>
>
> Yours, Kevan
>
>
> --
> Kevan Hashemi, Electrical Engineer
> Physics Department, Brandeis University
> http://alignment.hep.brandeis.edu/
>
_______________________________________________
Gpc mailing list
[email protected]
https://www.g-n-u.de/mailman/listinfo/gpc