comp.lang.ada
 help / color / mirror / Atom feed
* Re: Odd array dimension error GNAT
       [not found] <35dc97cd.17402128@news.geccs.gecm.com>
@ 1998-08-18  0:00 ` David C. Hoos, Sr.
       [not found]   ` <35db7934.1130426@news.geccs.gecm.com>
  0 siblings, 1 reply; 3+ messages in thread
From: David C. Hoos, Sr. @ 1998-08-18  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2799 bytes --]

Brian Orpin wrote in message <35dc97cd.17402128@news.geccs.gecm.com>...
>Trying to swap the dimensions of 2 arrays (if anyone has a simpler way
>feel free <G>).
>
>The query is why GNAT raises an error in the last set of loops where I
>have used a 'Last as opposed to the previous loop where I used a 'First +
>1.

Very simply because your code violates the RM, viz.:

3.6.2 Operations of Array Types

Legality Rules

1 The argument N used in the attribute_designators for the N-th dimension of
an array shall be a static expression of some integer type. The value of N
shall be positive (nonzero) and no greater than the dimensionality of the
array.

Static Semantics

2 The following attributes are defined for a prefix A that is of an array
type (after any implicit dereference), or denotes a constrained array
subtype:

3 A�First A�First denotes the lower bound of the first index range; its type
is the corresponding index type.
4 A�First(N) A�First(N) denotes the lower bound of the N-th index range; its
type is the corresponding index type.
5 A�Last A�Last denotes the upper bound of the first index range; its type
is the corresponding index type.
6 A�Last(N) A�Last(N) denotes the upper bound of the N-th index range; its
type is the corresponding index type.

7 A�Range A�Range is equivalent to the range A�First .. A�Last, except that
the prefix A is only evaluated once.
8 A�Range(N) A�Range(N) is equivalent to the range A�First(N) .. A�Last(N),
except that the prefix A is only evaluated once.
9 A�Length A�Length denotes the number of values of the first index range
(zero for a null range); its type is universal_integer.
10 A�Length(N) A�Length(N) denotes the number of values of the N-th index
range (zero for a null range); its type is universal_integer.

Since the dimensionality of your arrays is 2, N can only be 1 or 2.

Note also the following from the same section of the RM:

NOTES

12 45 The attribute_references A�First and A�First(1) denote the same value.
A similar relation exists for the attribute_references A�Last, A�Range, and
A�Length. The following relation is satisfied (except for a null array) by
the above attributes if the index type is an integer type:

13 A'Length(N) = A'Last(N) � A'First(N) + 1



Therefore your code (applicable fragment) should be:

Begin
   -- set some meaningful values
   For I In T1'Range(1) Loop
      For J In T1'Range(2) loop
     OLD_ARRAY(I,J) := COUNT;
     COUNT := COUNT + 1;
      End Loop;
   End Loop;

   -- swap the arrays
   NEW_ARRAY := SWAP (OLD_ARRAY);

   -- print the results
   For I In T2'Range(1) Loop
      For J In T2'Range(2) loop
     TEXT_IO.PUT(INTEGER'Image(NEW_ARRAY(I,J))&" ");
      End Loop;
   End Loop;

End TEST;


David C. Hoos, Sr.








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

* Re: Odd array dimension error GNAT
       [not found]   ` <35db7934.1130426@news.geccs.gecm.com>
@ 1998-08-19  0:00     ` David C. Hoos, Sr.
       [not found]       ` <35dfb078.15280421@news.geccs.gecm.com>
  0 siblings, 1 reply; 3+ messages in thread
From: David C. Hoos, Sr. @ 1998-08-19  0:00 UTC (permalink / raw)


Brian Orpin wrote in message <35db7934.1130426@news.geccs.gecm.com>...
>On Tue, 18 Aug 1998 14:49:34 -0500, "David C. Hoos, Sr."
><david.c.hoos.sr@ada95.com> wrote:
>
>>Brian Orpin wrote in message <35dc97cd.17402128@news.geccs.gecm.com>...
>>>Trying to swap the dimensions of 2 arrays (if anyone has a simpler way
>>>feel free <G>).
>
>>>The query is why GNAT raises an error in the last set of loops where I
>>>have used a 'Last as opposed to the previous loop where I used a 'First +
>>>1.
>
>>Very simply because your code violates the RM, viz.:
>
>How come the first one compiles?
>
><LRM snipped>
>
>>Since the dimensionality of your arrays is 2, N can only be 1 or 2.
>
>But I should still be able to use 'first and 'last as 'first should
>return 1 and 'last 2.

Not so. NOTE:
The value of T1'First is implicitly T1'First (1), and, therefore is1
The value of T1'Last is implicitly T1'Last (1), and, therefore is 3
The value of T2'First is implicitly T2'First (1), and, therefore is 1
The value of T2'Last is implicitly T2'Last (1), and, therefore is 10

The value of T1'First (2) is 1
The value of T1'Last (2) is 10
The value of T2'First (2) is 1
The value of T2'Last (2) is 3

If you don't believe it, print the Integer'Image oif each of
the above expressions to see what the values are.

>>Begin
>>   -- set some meaningful values
>>   For I In T1'Range(1) Loop
>>      For J In T1'Range(2) loop
>>     OLD_ARRAY(I,J) := COUNT;
>>     COUNT := COUNT + 1;
>>      End Loop;
>>   End Loop;
>
>But this is no less static than using 'first and ' last.

Agreed.  But staticness is not the issue here.  The values are plain wrong.

> It is also
>useless because it assumes that the array will always be of range 1..2.
>what if it was 0..1??
>

Wrong,  It assumes the _dimension_ (i.e., the number of subscripts)
of the arrays is always 2.  Is not this correct?

Perhaps another way of stating this is that there is no Ada attribute
returning the _dimensionality_ of an array.  The dimension to which the
attributes 'First, 'Last, 'Length and 'Range apply must be specified, either
explicitly or implicitly (in whch case the applicable dimension is the first
index).

>PS I forgot to mention that this is Ada 83 only so no clever 95 stuff
>thanks.

You did mention the Ada83 RM, and although I quoted from the Ada95 RM,
because that's what I have handy in electronic form, this aspect of the
language is unchanged.







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

* Re: Odd array dimension error GNAT
       [not found]       ` <35dfb078.15280421@news.geccs.gecm.com>
@ 1998-08-19  0:00         ` David C. Hoos, Sr.
  0 siblings, 0 replies; 3+ messages in thread
From: David C. Hoos, Sr. @ 1998-08-19  0:00 UTC (permalink / raw)



Brian Orpin wrote in message <35dfb078.15280421@news.geccs.gecm.com>...
>On Wed, 19 Aug 1998 05:14:35 -0500, "David C. Hoos, Sr."
><david.c.hoos.sr@ada95.com> wrote:
>
>Doh! if I had declared the range to be 1..2 in the first place as I
>intended then it would have worked............
>Thanks
>
Yes, but the code would still have been "wrong."  Suppose one or
both of the index types had been enumerations (e.g., the days of
the week), then the attributes 'First, 'Last, and 'Range would yield
enumeration values.  Then 'First + 1 would be illegal.

Furthermore, the expression inside the parenthesis must be
a positive value (within the dimensions -- not the index ranges -- of the
array).

David C. Hoos, Sr.








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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <35dc97cd.17402128@news.geccs.gecm.com>
1998-08-18  0:00 ` Odd array dimension error GNAT David C. Hoos, Sr.
     [not found]   ` <35db7934.1130426@news.geccs.gecm.com>
1998-08-19  0:00     ` David C. Hoos, Sr.
     [not found]       ` <35dfb078.15280421@news.geccs.gecm.com>
1998-08-19  0:00         ` David C. Hoos, Sr.

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