I should be able to pass zero size arrays to a function. But the compiler removes the call, with the comment:
comment 338 - This expression contains a zero-sized rank, so will do nothing
However, this is not the correct behaviour according to the standard. One might want to do something useful if the array is zero size. In particular, one might want to write defensive code which copes with such cases.
I have checked the standard, and you should definitely allow zero sized arrays to be passed.
I discovered this while implementing quickselect.
Example code (quick select with parts removed):
module statistics
contains
! Finds kth smallest (i.e. kth order statistic) amongst an array of values
function kth_smallest(x, k)
integer, intent(in) :: k
real, intent(in) :: x(:)
real kth_smallest
! Return maximum value if k is larger then the array size.
! This special case should also be triggered if the array has zero size
! in which case, maxval should return -HUGE(1.0) for the default real
! kind.
if (k > size(x)) then
kth_smallest = maxval(x)
return
end if
! Return minimum value if k == 1
if (k == 1) then
kth_smallest = minval(x)
return
end if
! Assert 1<k <= size(x).
! Find kth order statistic using the quickselect algorithm
!** Code not shown
end function kth_smallest
end module statistics
program test
use statistics
real :: x(7) = (/ 1.0, 3.0, 2.0, 4.0, 7.0, 6.0, 5.0/)
real :: y = 0.0
! Test with zero size array section
y = kth_smallest(x(2:1), 3) !!<< Bug. When I step with the debugger this is not called.
! Should print -Huge(1.0) but doesn't
print *, y
end program test
David.