[J3] Lower bound of an allocatable array function result
Steidel, Jon L
jon.l.steidel at intel.com
Fri Jun 14 12:23:20 EDT 2019
5.4.10 Allocatable variables p3 states:
"If an allocatable variable is an array, the rank is declared, but the bounds are determined when it is allocated."
9.7.1.2 Execution of an ALLOCATE statement p1 states:
"When an allocate statement is executed for an array for which <allocate-shape-spec-list> is specified, the values
of the lower and upper bound expressions determine the bounds of the array."
10.2.1.3 Interpretation of intrinsic assignments p3 states:
"If the variable is or becomes an unallocated allocatable variable, it is then allocated with
* . . .
* The shape of expr with each lower bound equal to the corresponding element of LBOUND (expr) if
expr is an array.
15.5.3 Function reference p1 states:
"When execution of the function is complete, the value of the function result is available for use in the expression
that caused the function to be invoked. The characteristics of the function result are determined by the interface
of the function."
15.3.3 Characteristics of function results states p1 states:
"If a function result is an array that is not allocatable or a pointer, its shape is a characteristic.
If a type parameter of a function result or a bound of a function result array is not a constant expression, the
exact dependence on the entities in the expression is a characteristic."
This all leads me to believe that if an allocatable array function result is allocated with lower bounds other than 1, and
the result of that function is assigned to an unallocated allocatable array, the variable in the assignment statement should
be allocated with the same bounds that the function result variable was allocated with.
I have access to three compilers. When I compile and execute the test below, I get the same results from two of the
compilers, and a segmentation fault from the third. But the results show the function result lower bound, and the lower
bound of the allocatable array that the function result is assigned to is 1.
Are the two compilers wrong, or am I missing something about the lower bounds of the result of an array valued function?
PROGRAM main
INTEGER :: x(-1:1) = [ 1, 2, 3 ]
INTEGER, ALLOCATABLE :: y(:)
INTEGER, ALLOCATABLE :: z(:)
y = x
PRINT *, "lbound(y) = ", LBOUND(y), "; y = ", y
PRINT *, "lbound(f()) = ", LBOUND(f1())
z = f1()
PRINT *, "lbound(z) = ", LBOUND(z), "; z = ", z
DEALLOCATE (z)
PRINT *, "lbound(f()) = ", LBOUND(f2())
z = f2()
PRINT *, "lbound(z) = ", LBOUND(z), "; z = ", z
CONTAINS
FUNCTION f1() RESULT( r )
INTEGER,DIMENSION(-3:-1) :: r
r = [1, 2, 3]
END FUNCTION
FUNCTION f2() RESULT( r )
INTEGER, ALLOCATABLE :: r(:)
ALLOCATE ( r(-1:1))
r = [1, 2, 3]
END FUNCTION
END PROGRAM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20190614/50c280cb/attachment-0001.html>
More information about the J3
mailing list