comp.lang.ada
 help / color / mirror / Atom feed
* Re: Thanks Tucker, It works !!!
@ 1998-09-20  0:00 tmoran
  0 siblings, 0 replies; 4+ messages in thread
From: tmoran @ 1998-09-20  0:00 UTC (permalink / raw)


> But I had to do it with an access type per design requirements.
The example is a Patriot missile to shoot down a fly.  One trusts
there's a reason for this complexity in the actual app.

> I still don't really understand the aliased, all, and access
> keywords working together to allow this sort of C-like pointer
Here's one attempt at an explanation:

  Your Object contains, among other things (eg, abc) a set of 100
pointers to variable sized arrays of integers.  These arrays are not
created dynamically by 'new' allocators, but are in fact aliases of
ordinary arrays declared statically (eg xyz5_array), thus allowing
'back door' access to those arrays and the resulting maintenance,
optimization, storage/register placement etc problems of things that
can be read or written directly (xyz5_array(3) := 17;) or indirectly
(This.XYZ(1).all(3) := 17;).  The key word 'aliased' gives warning to
the compiler and the maintainer that such shenanigans are going on
with xyz5_array.

  Usually, access types point to things dynamically allocated on the
heap with 'new'.  But your type "xyz_array_pointer" may in fact point
to something allocated on the stack, like xyz5_array, as well.  The
keyword 'all' tells the compiler and the maintainer this vital fact
about xyz_array_pointer.

  An Ada 83 access type only pointed to heap-allocated objects, so it
didn't have 'all' or 'aliased'.  Basically, to make your program run
in Ada 83, you would have to simplify it by dropping those key words
and the line
  xyz5_array : aliased xyz_array := (1..5 => 0);
and changing
  This.XYZ(1) := xyz5_array'ACCESS;  -- address of xyz5_array is assigned
to
  This.XYZ(1) := new xyz_array'((1..5 => 0)); -- an xyz5 array is created

  Your next question, should you choose to continue this task, will
probably be about the error messages you get when forgetting about the
lifetime problems of pointers to things that disappear when the stack
is popped on return from a subprogram.




^ permalink raw reply	[flat|nested] 4+ messages in thread
* Thanks Tucker, It works !!!
@ 1998-09-19  0:00 Technobabble
  1998-09-20  0:00 ` dewarr
  0 siblings, 1 reply; 4+ messages in thread
From: Technobabble @ 1998-09-19  0:00 UTC (permalink / raw)


Greetings,

The final code, which I compiled with GNAT, outputs the following:

test_pkg
test_pkg
test_pkg
test_pkg
test_pkg

Basically, I wanted to specify an unconstrained array in a spec for a user
and then let the user constrain it.  Then a procedure, also in the spec,
could determine the size of the array, in this case 5, since the loop
executed 5 times. But I had to do it with an access type per design
requirements.

Below is the final code, I still don't really understand the aliased, all,
and access keywords working together to allow this sort of C-like pointer
operation, but Ada95 does allow it.  Anyone care to explain, I'd love to
learn the reasons behind allowing pointer operations.

Question:  would this work in Ada83 not having these keywords???


Here is the final code:



****
file test_pkg.ads
****
package test_pkg is

 type xyz_array is array (integer range <>) of integer;   -- this is it

 type xyz_array_pointer is access all xyz_array;

 type xyz_array_pointer_array is array (1..100) of xyz_array_pointer;


 type Object is
     record
        XYZ : xyz_array_pointer_array;    
        abc : integer;
     end record;

 -- user can define this array externally, just here for demo
 xyz5_array : aliased xyz_array := (1..5 => 0);

 procedure my_range (This : in out Object); 

end test_pkg;



****
file: my_main.adb
****
with test_pkg; use test_pkg;
procedure my_main is
   my_this : Object;
begin
   my_range (my_this);
end my_main;



****
file: test_pkg.adb
****

with Text_IO; use Text_IO;
package body test_pkg is

procedure my_range (This : in out Object) is
begin 
        -- next line can be externally done by user, here for demo
        This.XYZ(1) := xyz5_array'ACCESS;  -- address of xyz5_array is assigned
        for I in This.XYZ(1)'RANGE
        loop
          put_line ("test_pkg");
        end loop;

end my_range;

end test_pkg;




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1998-09-20  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-09-20  0:00 Thanks Tucker, It works !!! tmoran
  -- strict thread matches above, loose matches on Subject: below --
1998-09-19  0:00 Technobabble
1998-09-20  0:00 ` dewarr
1998-09-20  0:00   ` Technobabble

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox