[J3] Lower bound of an allocatable array function result
Daniel C Chen
cdchen at ca.ibm.com
Fri Jun 14 13:48:16 EDT 2019
I got:
> a.out
lbound(y) = -1 ; y = 1 2 3
lbound(f()) = 1
lbound(z) = -3 ; z = 1 2 3
lbound(f()) = 1
lbound(z) = -1 ; z = 1 2 3
I think 10.2.1.3 Interpretation of intrinsic assignment defines the low
bound of y should be the same as the "expr".
Thanks,
Daniel
XL Fortran Development, Fortran Standard Representative
IBM Toronto Software Lab
Phone: 905-413-3056
Tie: 969-3056
Email: cdchen at ca.ibm.com
http://www.ibm.com/software/awdtools/fortran/xlfortran
From: "Steidel, Jon L via J3" <j3 at mailman.j3-fortran.org>
To: General J3 interest list <j3 at mailman.j3-fortran.org>
Cc: "Steidel, Jon L" <jon.l.steidel at intel.com>, "Shu, Michael"
<michael.shu at intel.com>
Date: 06/14/2019 12:28 PM
Subject: [EXTERNAL] [J3] Lower bound of an allocatable array function
result
Sent by: "J3" <j3-bounces at mailman.j3-fortran.org>
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/6e3ed5dc/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: graycol.gif
Type: image/gif
Size: 105 bytes
Desc: not available
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20190614/6e3ed5dc/attachment.gif>
More information about the J3
mailing list