comp.lang.ada
 help / color / mirror / Atom feed
* Using Access Types for a simple solution
@ 2016-07-25 13:35 ldries46
  2016-07-25 14:28 ` Dmitry A. Kazakov
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: ldries46 @ 2016-07-25 13:35 UTC (permalink / raw)


Let me first explain the problem. I do have a matrix of records

type Box is record
   ...
   ...
end record

Grid : array( 1 .. N,  1 .. M) of Box;

Now I must do something with a random set out of elements from the matrix. I 
would like to create an array of pointers to the elements of the matrix so 
that I can use these elements and file the changes directly if the array. So 
I created a type:

type Box_Access is access all Box;

and make an array

Box_Array : array(1 .. Q) of Box_Acess;

When try to use that in a simple statement

Box_Array(n) := Grid(n1, n2)'Access;

I get the error (only for this statement): prefix of 'Access attribute must 
be aliased.

I tried:

Grid : aliased array( 1 .. N,  1 .. M) of Box;

But that also gives the same error.
I cannot imagine that what I want is not possible in Ada as I can realise it 
in C++ so I do miss something.

L. Dries








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

* Re: Using Access Types for a simple solution
  2016-07-25 13:35 Using Access Types for a simple solution ldries46
@ 2016-07-25 14:28 ` Dmitry A. Kazakov
  2016-07-25 15:27 ` ldries46
  2016-07-26 18:16 ` Aurele
  2 siblings, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2016-07-25 14:28 UTC (permalink / raw)


On 2016-07-25 15:35, ldries46 wrote:
> Let me first explain the problem. I do have a matrix of records
>
> type Box is record
>   ...
>   ...
> end record
>
> Grid : array( 1 .. N,  1 .. M) of Box;
>
> Now I must do something with a random set out of elements from the
> matrix. I would like to create an array of pointers to the elements of
> the matrix so that I can use these elements and file the changes
> directly if the array. So I created a type:
>
> type Box_Access is access all Box;
>
> and make an array
>
> Box_Array : array(1 .. Q) of Box_Acess;
>
> When try to use that in a simple statement
>
> Box_Array(n) := Grid(n1, n2)'Access;
>
> I get the error (only for this statement): prefix of 'Access attribute
> must be aliased.
>
> I tried:
>
> Grid : aliased array( 1 .. N,  1 .. M) of Box;
>
> But that also gives the same error.
> I cannot imagine that what I want is not possible in Ada as I can
> realise it in C++ so I do miss something.

Yes, the error message that tells you: "prefix of 'Access attribute must 
be aliased". The prefix of 'Access attribute in Grid(n1, n2)'Access is 
obviously Grid(n1, n2).

Aliased array /= array of aliased elements.

And you should not use pointers anyway. Do sets of array indices instead.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: Using Access Types for a simple solution
  2016-07-25 13:35 Using Access Types for a simple solution ldries46
  2016-07-25 14:28 ` Dmitry A. Kazakov
@ 2016-07-25 15:27 ` ldries46
  2016-07-25 21:39   ` rieachus
  2016-07-26 18:16 ` Aurele
  2 siblings, 1 reply; 5+ messages in thread
From: ldries46 @ 2016-07-25 15:27 UTC (permalink / raw)


Thanks Dimitri,

I did not realise that the aliased not before the array comes but before the 
box.
The suggestion you did I tried before but the code became to complicated to 
be still readable.
May be this not the best way to tackle my problem but is is better readable 
and therefore better maintainable

L. Dries

"ldries46"  schreef in bericht news:579615c5$0$20671$e4fe514c@news.kpn.nl...

Let me first explain the problem. I do have a matrix of records

type Box is record
   ...
   ...
end record

Grid : array( 1 .. N,  1 .. M) of Box;

Now I must do something with a random set out of elements from the matrix. I
would like to create an array of pointers to the elements of the matrix so
that I can use these elements and file the changes directly if the array. So
I created a type:

type Box_Access is access all Box;

and make an array

Box_Array : array(1 .. Q) of Box_Acess;

When try to use that in a simple statement

Box_Array(n) := Grid(n1, n2)'Access;

I get the error (only for this statement): prefix of 'Access attribute must
be aliased.

I tried:

Grid : aliased array( 1 .. N,  1 .. M) of Box;

But that also gives the same error.
I cannot imagine that what I want is not possible in Ada as I can realise it
in C++ so I do miss something.

L. Dries






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

* Re: Using Access Types for a simple solution
  2016-07-25 15:27 ` ldries46
@ 2016-07-25 21:39   ` rieachus
  0 siblings, 0 replies; 5+ messages in thread
From: rieachus @ 2016-07-25 21:39 UTC (permalink / raw)


On Monday, July 25, 2016 at 11:28:22 AM UTC-4, ldries46 wrote:
> Thanks Dimitri,
> 
> I did not realise that the aliased not before the array comes but before the 
> box.
> The suggestion you did I tried before but the code became to complicated to 
> be still readable.
> May be this not the best way to tackle my problem but is is better readable 
> and therefore better maintainable

I first read this hours ago, then left it to simmer while I went to a doctor's appointment.  I am sure solving your programming problem in Ada is easy.  What you need to learn first though is how to think about problems to be solved in Ada.

It has been over 25 years since I tripped over the most important rule of software engineering (not programming!) in Ada.  Model the problem space, not the solution.  There may be lots of programs needed in a given problem space, and you quickly learn that requirements change rapidly, even after they have been frozen. ;-(

All I know so far is that you have a collection of records (called Boxes) and it may make sense to address them by row and column, or by pointer (access value in Ada).  What activities make sense for your Boxes?  Creation? modifying contents? sort? on what key? print? write to a file?  Have them chase each other around the screen?

There are other possibilities.  The Ada standard library has lots of different types of collections depending on what you need to do with them.  It may be that a two-dimensional array is what the requirements assume. (But you know what I think about requirement documents.) 


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

* Re: Using Access Types for a simple solution
  2016-07-25 13:35 Using Access Types for a simple solution ldries46
  2016-07-25 14:28 ` Dmitry A. Kazakov
  2016-07-25 15:27 ` ldries46
@ 2016-07-26 18:16 ` Aurele
  2 siblings, 0 replies; 5+ messages in thread
From: Aurele @ 2016-07-26 18:16 UTC (permalink / raw)


Maybe try something like this (just a suggestion):

procedure Matric_Demo is

  type Matrix is record
    x : integer := 0;
    y : integer := 0;
    z : integer := 0;
    t : integer := 0;
        :
  end record;
  type Matrix_Ptr is access all Matrix;
  
  type Grid is array ( 1..Integer'last, 1..integer'last ) of Matrix_Ptr;
  type Grid_Ptr is access all Grid;
  
  My_Matrix_Ptr : Matrix_Ptr := new Matrix;

  My_Grid_Ptr_1 : Grid_Ptr := new Grid; 
  My_Grid_Ptr_2 : Grid_Ptr := new Grid; 
     :
  My_Array      : array ( 1 .. integer'last ) of Grid_Ptr; 
  
begin

  My_Matrix_Ptr.x := 1;
  My_Matrix_Ptr.y := 2;
  My_Matrix_Ptr.y := 3;
  My_Matrix_Ptr.y := 4;
     :
  My_Grid_Ptr_1( 1, 1 ) := My_Matrix_Ptr;
     :
  My_Array( 1 ) := My_Grid_Ptr_1;
     :
end Matric_Demo;


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

end of thread, other threads:[~2016-07-26 18:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-25 13:35 Using Access Types for a simple solution ldries46
2016-07-25 14:28 ` Dmitry A. Kazakov
2016-07-25 15:27 ` ldries46
2016-07-25 21:39   ` rieachus
2016-07-26 18:16 ` Aurele

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