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

Van Snyder van.snyder at sbcglobal.net
Tue Nov 30 23:09:50 UTC 2021


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/1a3e06d7/attachment.htm>


More information about the J3 mailing list