Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29246 Discussions

Question on the bounds of an allocatable function result

alexandross
Beginner
451 Views
Hello,

I'm using the following compiler:

Intel Fortran Intel 64 Compiler XE for applications running on Intel 64, Version 12.1.1.256 Build 20111011

Take a look at the following code:

[fortran]program test
implicit none

real, allocatable, dimension(:,:) :: a1, a2

allocate(a1(0:6,0:4))
a1 = 0.
a2 = copy_array(a1)

print *, shape(a1)
print *, lbound(a1,1), lbound(a1,2)
print *, ubound(a1,1), ubound(a1,2)
print *
print *, shape(a2)
print *, lbound(a2,1), lbound(a2,2)
print *, ubound(a2,1), ubound(a2,2)

contains

function copy_array (array) result(copy)
real, allocatable, dimension(:,:) :: copy
real, allocatable, dimension(:,:), intent(in) :: array
copy = array
end function copy_array

end program test
[/fortran]
The results, if the program is compiled with the -assume realloc_rhs option, are the following:

[plain]           7           5
           0           0
           6           4
 
           7           5
           1           1
           7           5
[/plain]

My focus here is on the bounds of the arrays. At first the allocatable array a1 is allocated using an lbound of 0. Then a1 is passed to the function "copy_array", through the dummy array "array", which is declared to be allocatable. Therefore, its lbound of zero is retained within the function, which I verified using the debugger. This is in accordance with what I read about allocatable dummy arguments in the book of Metcalf et al Modern fortran explained. Then, the function result "copy", which is also allocatable, is defined using the assignement "copy=array", which makes the lbound of "copy" also equal to zero (fortran 2003 rules apply due to the -assume realloc_rhs compilation option). This I also verified using the debugger.

However, when the function returns the result of the function has an lbound of 1, which causes the lbound of a2 also to be 1.

My question is: Is this normal behaviour according the the fortran standard? In "Modern Fortran explained" (or previous versions) there is a paragraph titled "Allocatable functions" (section 6.5.5) but this issue is not clarified. That is, can the bounds of allocatable function results be defined within the function, or will their lbounds always be 1? It would be useful if the bounds of the results could be defined, because that would allow to define the bounds of allocatable arrays through statements such as "a2 = copy_array(a1)" of the above code.

Thank you,

Alexandros
0 Kudos
2 Replies
IanH
Honored Contributor III
451 Views
The behaviour you see is as per the standard. The lower bound given to var when reallocating on assignment for var = expr is what is given by LBOUND(expr). When LBOUND is applied to an expression that is not a whole array (as in this case - the primary that makes up the expression is a function reference) the result is one. Hence the lower bound of the allocated variable after the assignment should be one.

If you want the bounds to be defined by a procedure then consider using a subroutine and passing the allocatable variable as a actual argument to an allocatable dummy argument.
0 Kudos
alexandross
Beginner
451 Views
OK thanks for the reply. I'll use a subroutine instead, with the array passed with intent(out). It would be nice though if a function with an allocatable result would provide full control over the characteristics of the result, including bounds.
0 Kudos
Reply