[J3] ALLOCATE with source-expr determining the array bound & resulting lower bound

Steidel, Jon L jon.l.steidel at intel.com
Tue Nov 30 23:41:02 UTC 2021


I believe the point Tobias was making is the definition of LBOUND states:

RESULT VALUE.

Case (i):                If DIM is present, ARRAY is a whole array, and either ARRAY is an assumed-size array of rank DIM or dimension DIM of ARRAY has nonzero
                                extent, the result has a value equal to the lower bound for subscript DIM of ARRAY.  Otherwise, if DIM is present, the result is 1.
Case(ii):                LBOUND (ARRAY) has a value whose ith element is equal to LBOUND (ARRAY, i), for i = 1, 2, . . ., n, where n is the rank of ARRAY.
                                LBOUND (ARRAY, KIND=KIND) has the value whose ith element is equal to LBOUND (ARRAY, i, kind), for i = 1, 2, . . . ,n, where
                                n is the rank of ARRAY.

Whole array is defined as:
                array component or array name without further qualification

h(3) is a function result, which is not an array component or array name.  Therefore, the “otherwise” in Case (i) applies, and the lower bound of c is 1.

From: J3 <j3-bounces at mailman.j3-fortran.org> On Behalf Of Van Snyder via J3
Sent: Tuesday, November 30, 2021 5:10 PM
To: General J3 interest list <j3 at mailman.j3-fortran.org>
Cc: Van Snyder <van.snyder at sbcglobal.net>; Harald Anlauf <anlauf at gmx.de>; Chung-Lin Tang <cltang at codesourcery.com>
Subject: Re: [J3] ALLOCATE with source-expr determining the array bound & resulting lower bound

On Tue, 2021-11-30 at 21:41 +0100, Tobias Burnus via J3 wrote:
Hello J3,

I have a question regarding the following which came up for GCC.

* NAG, Nvidia, flang yield a lower bounds of 'c' of 3 in the following example.
* ifort gives a lower bound of 1
* When fixing a related issue, GCC is about to change from 3 to 1
   as collateral change.

However, my question is which value is the proper lower bound
of 'c' for:

implicit none
  integer, allocatable :: c(:)
  allocate(c, source=h(3))
  write(*,*) lbound(c,1), ubound(c,1)
contains
  pure function h(i) result(r)
   integer, value, intent(in) :: i
   integer, allocatable :: r(:)
   allocate(r(3:5))
   r = [1,2,3]
  end function h
end


18-007r1 [134:27-29] has the following (and 21-007r2 [144:3-5] likewise):
"When an ALLOCATE statement is executed for an array with no
  allocate-shape-spec-list, the bounds of source-expr determine
  the bounds of the array."

Unless I missed something, the standard never quite says what the
bounds of an expression is. For the lower bound, I see it at two
places: In the LBOUND intrinsic itself – and at several places in
the standard, there is wording like the following (here for the
intrinsic assignment, 10.2.1.3, [161:17-18]):
"the shape of expr with each lower bound equal to the corresponding
  element of LBOUND (expr) if expr is an array."

I do not see such wording for the ALLOCATE with the source-expr
and wonder whether it is missing.


Assuming the LBOUND rules apply, there is in 16.9.109 LBOUND [384:29-31]:
'ARRAY is a whole array, and ... Otherwise, if DIM is present, the result
value is 1.'

9.4.2 defines 'A whole array is a named array or a structure
component whose final part-ref is an array component name ...' [127:6-7]

And my reading is that  h(3)  returns an array but that one is not named;
thus, in my reading lbound(h(3),1) = 1. – And, using the LBOUND connection,
I infer lbound(c,1) = 1.

First question: Have I missed something?
Second question: Is some LBOUND wording needed for source-expr?

Tobias

The result value of h(3) is allocatable and therefore its bound(s) are the ones specified when it was allocated (15.3.3, 15.5.3). I would expect the lower bound of h(3) to be 3, and the lower bound of c to be 3.

If the allocate statement were changed to


allocate ( c, source = ( h(3) ) )


then the lower bound would be 1 because ( h(3) ) is not an allocatable object.

I remember reading something about an allocatable result of a function not being deallocated until after the result is used, for example as the <expr> in an assignment, an item in an output list, an actual argument, ..., but I can't find it now.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20211130/ff000216/attachment-0001.htm>


More information about the J3 mailing list