comp.lang.ada
 help / color / mirror / Atom feed
* unconstrained array clarification
@ 1998-09-19  0:00 Technobabble
  1998-09-19  0:00 ` dewarr
  1998-09-19  0:00 ` Tucker Taft
  0 siblings, 2 replies; 6+ messages in thread
From: Technobabble @ 1998-09-19  0:00 UTC (permalink / raw)


Ok, thanks !  Now what if all I have is an unconstrained array type like this:

type xyz_array is array (integer <>) of integer;   -- this is it
type xyz_array_pointer is access 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;


Now I declare an Object,

This : Object;   --  record is not constrained, legal or not?

Now I want to have say a 5 element xyz_array:

xyz5_array : xyz_array (1..5);

and I assign my pointer

This.XYZ(1) := xyz5_array'ACCESS;  -- address of xyz_array type is assigned

now I want to loop:

for I in This.XYZ(1)'RANGE
     loop 
      ..........


???? Will this work ???  Is the syntax totally incorrect ???

help !!!

Richmond




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

* Re: unconstrained array clarification
  1998-09-19  0:00 unconstrained array clarification Technobabble
  1998-09-19  0:00 ` dewarr
@ 1998-09-19  0:00 ` Tucker Taft
  1998-09-19  0:00   ` Technobabble
  1 sibling, 1 reply; 6+ messages in thread
From: Tucker Taft @ 1998-09-19  0:00 UTC (permalink / raw)


Technobabble (WishList@2600.com) wrote:

: Ok, thanks !  Now what if all I have is an unconstrained array type like this:

: type xyz_array is array (integer <>) of integer;   -- this is it
                                  ^^ insert "range"

: type xyz_array_pointer is access xyz_array;
                                  ^^ insert "all" if you want to use 'Access

: 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;


: Now I declare an Object,

: This : Object;   --  record is not constrained, legal or not?

This record *is* constrained since its type doesn't have any discriminants.
If its type did have discriminants, this would be legal if the
discriminants had defaults.
                        
: Now I want to have say a 5 element xyz_array:

: xyz5_array : xyz_array (1..5);

: and I assign my pointer

: This.XYZ(1) := xyz5_array'ACCESS;  -- address of xyz_array type is assigned

This is not legal for two reasons.  First, xyz5_array needs
to be declared "aliased".  Secondly, the "nominal subtype" of
xyz5_array must exactly match the "designated" subtype of the access
type.  Since the designated subtype is xyz_array (unconstrained),
then xyz5_array needs to have an unconstrained "nominal" subtype, as follows:

   xyz5_array : aliased xyz_array := (1..5 => 0);

This requirement for subtype matching is a bit of a pain, but using 'Access
imposes some stringent requirements on matching of representation, and
for pointers to arrays, it can matter greatly whether the nominal
subtype is constrained or unconstrained (you'll have to trust me on
this one).

: now I want to loop:

: for I in This.XYZ(1)'RANGE
:      loop 
:       ..........


: ???? Will this work ???  Is the syntax totally incorrect ???

This can be made to work, with the above changes.

It might be interesting to have some idea where you are headed
with all of this.  There might be a simpler overall solution.

: help !!!

: Richmond

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA
An AverStar Company




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

* Re: unconstrained array clarification
  1998-09-19  0:00 unconstrained array clarification Technobabble
@ 1998-09-19  0:00 ` dewarr
  1998-09-19  0:00 ` Tucker Taft
  1 sibling, 0 replies; 6+ messages in thread
From: dewarr @ 1998-09-19  0:00 UTC (permalink / raw)


In article <WishList-1809982255060001@a17.phoenix-14.goodnet.com>,
  WishList@2600.com (Technobabble) wrote:
> Ok, thanks !  Now what if all I have is an unconstrained array type like
this:
>
> type xyz_array is array (integer <>) of integer;   -- this is it
> type xyz_array_pointer is access 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;
>
> Now I declare an Object,
>
> This : Object;   --  record is not constrained, legal or not?
>
> Now I want to have say a 5 element xyz_array:
>
> xyz5_array : xyz_array (1..5);
>
> and I assign my pointer
>
> This.XYZ(1) := xyz5_array'ACCESS;  -- address of xyz_array type is assigned
>
> now I want to loop:
>
> for I in This.XYZ(1)'RANGE
>      loop
>       ..........
>
> ???? Will this work ???  Is the syntax totally incorrect ???
>
> help !!!
>
> Richmond



One useful rule for you at this stage is NEVER EVER use the
access attribute. It is for specialized uses, but should
hardly be used at all in normal programming. If you feel
the urge to use it at this level, you have not got the
proper semantic feel for Ada, but are trying instead to
write C in Ada, much as a Fortran-66 programmer might end
up writing lots of gotos in Ada.

Just use the rule for yourself that Ada does not have an
access attribute, and figure out how to solve your problem,
you will learn much faster that way.

Then when you really understand why the access attribute is
almost never used, you will be ready to learn about the rare
cases in which its use is justified.

By the way, your example code contains no discriminants,
so talking about whether your records are constrained or
unconstrained is completely wrong. If there are no
discriminants around, then of course all your records
are constrained.

It is VERY important to get the terminology right here. Go
back to Barnes, and make sure you understand these terms!


-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: unconstrained array clarification
  1998-09-19  0:00 ` Tucker Taft
@ 1998-09-19  0:00   ` Technobabble
  1998-09-19  0:00     ` dewarr
  1998-09-22  0:00     ` Robert I. Eachus
  0 siblings, 2 replies; 6+ messages in thread
From: Technobabble @ 1998-09-19  0:00 UTC (permalink / raw)


Greetings Tucker,

Thanks for the excellent response to my problem.  Thanks also for being
kind enough to see through my novice attempts at syntax and patient enough
to really understand and attack this problem. I'm sorry for the late night
off the cuff coding examples that introduced misleading errors and
produced a frustrated audience, it's all my fault, sorry. Thanks for
seeing through all of that !!!


recall:

>    xyz5_array : aliased xyz_array := (1..5 => 0);
> 
> This requirement for subtype matching is a bit of a pain, but using 'Access
> imposes some stringent requirements on matching of representation, and
> for pointers to arrays, it can matter greatly whether the nominal
> subtype is constrained or unconstrained (you'll have to trust me on
> this one).
> 
> : now I want to loop:
> 
> : for I in This.XYZ(1)'RANGE
> :      loop 
> :       ..........
> 
> 
> : ???? Will this work ???  Is the syntax totally incorrect ???
> 
> This can be made to work, with the above changes.
> 
> It might be interesting to have some idea where you are headed
> with all of this.  There might be a simpler overall solution.
> 

What I want to do is to keep the addresses for several arrays of records
of different lengths in an array of pointers. Then write the contents of
each record to I/O.  The problem is to do this with only the array of
pointers, if I don't want to keep track of the size of each array (number
of records), and I want to use the RANGE attribute to determine the size. 
But the only way I have of getting to the array of records is via the
pointer.  I didn't design this stuff either, it's adopted from legacy
code. I'll re-code the stuff in GNAT with the changes you've supplied and
see if I can get a good compile.

Thanks,
Richmond




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

* Re: unconstrained array clarification
  1998-09-19  0:00   ` Technobabble
@ 1998-09-19  0:00     ` dewarr
  1998-09-22  0:00     ` Robert I. Eachus
  1 sibling, 0 replies; 6+ messages in thread
From: dewarr @ 1998-09-19  0:00 UTC (permalink / raw)


In article <WishList-1909981042300001@a1.phoenix-7.goodnet.com>,
  WishList@2600.com (Technobabble) wrote:
> Greetings Tucker,
>
> Thanks for the excellent response to my problem.  Thanks also for being
> kind enough to see through my novice attempts at syntax and patient enough
> to really understand and attack this problem. I'm sorry for the late night
> off the cuff coding examples that introduced misleading errors and
> produced a frustrated audience, it's all my fault, sorry. Thanks for
> seeing through all of that !!!
>
> recall:
>
> >    xyz5_array : aliased xyz_array := (1..5 => 0);
> >
> > This requirement for subtype matching is a bit of a pain, but using 'Access
> > imposes some stringent requirements on matching of representation, and
> > for pointers to arrays, it can matter greatly whether the nominal
> > subtype is constrained or unconstrained (you'll have to trust me on
> > this one).
> >
> > : now I want to loop:
> >
> > : for I in This.XYZ(1)'RANGE
> > :      loop
> > :       ..........
> >
> >
> > : ???? Will this work ???  Is the syntax totally incorrect ???
> >
> > This can be made to work, with the above changes.
> >
> > It might be interesting to have some idea where you are headed
> > with all of this.  There might be a simpler overall solution.
> >
>
> What I want to do is to keep the addresses for several arrays of records
> of different lengths in an array of pointers. Then write the contents of
> each record to I/O.  The problem is to do this with only the array of
> pointers, if I don't want to keep track of the size of each array (number
> of records), and I want to use the RANGE attribute to determine the size.
> But the only way I have of getting to the array of records is via the
> pointer.  I didn't design this stuff either, it's adopted from legacy
> code. I'll re-code the stuff in GNAT with the changes you've supplied and
> see if I can get a good compile.
>
> Thanks,
> Richmond


Seems odd that this could be legacy code, since Ada 83 had no facilities
comparable to 'Access (at least not in reasonable coding styles).

>

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: unconstrained array clarification
  1998-09-19  0:00   ` Technobabble
  1998-09-19  0:00     ` dewarr
@ 1998-09-22  0:00     ` Robert I. Eachus
  1 sibling, 0 replies; 6+ messages in thread
From: Robert I. Eachus @ 1998-09-22  0:00 UTC (permalink / raw)


In article <WishList-1909981042300001@a1.phoenix-7.goodnet.com> WishList@2600.com (Technobabble) writes:

  > What I want to do is to keep the addresses for several arrays of records
  > of different lengths in an array of pointers. Then write the contents of
  > each record to I/O.  The problem is to do this with only the array of
  > pointers, if I don't want to keep track of the size of each array (number
  > of records), and I want to use the RANGE attribute to determine the size. 
  > But the only way I have of getting to the array of records is via the
  > pointer.  I didn't design this stuff either, it's adopted from legacy
  > code. I'll re-code the stuff in GNAT with the changes you've supplied and
  > see if I can get a good compile.

   What you want to do is to declare an access type which is your
object type:

   type XYZ is private;
   type XYZ_Array is array(Natural range <>) of XYZ;
   ...
  private

   type XYZ_Object is array(Natural range <>) of Foo;
   type XYZ is access XYZ_Object;

   If Foo is Character, you can use Ada.Strings.Unbounded, otherwise
you can use the GNAT implementation to see how to make it all work.
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-09-19  0:00 unconstrained array clarification Technobabble
1998-09-19  0:00 ` dewarr
1998-09-19  0:00 ` Tucker Taft
1998-09-19  0:00   ` Technobabble
1998-09-19  0:00     ` dewarr
1998-09-22  0:00     ` Robert I. Eachus

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