Invocation of subroutine and dimension of dummy array

I am wondering whether this is legal or not according to the standard, i.e. passing an array to a subroutine with a small size without an explicit slice.
Neither gfortran -Wall -pedantic -fbounds-check nor valgrind show any error, but I am not sure that providing the actual slice would be needed.

subroutine foobar(x,n)
implicit none
integer, intent(in) :: n
integer, intent(in) :: x(n)
integer :: i

write(*,*) "foobar"

do i = 1, n
  write(*,*) i, x(i)
end do

end subroutine foobar

program main
implicit none
integer, parameter :: s = 5
integer :: dat(s)
integer :: i

interface
  subroutine foobar(x,n)
    implicit none
    integer, intent(in) :: n
    integer, intent(in) :: x(n)
  end subroutine foobar
end interface

dat = [10,20,30,40,50]

write(*,*) "main"
do i = 1, s
  write(*,*) i, dat(i)
end do

call foobar(dat,s)     ! OK since actual size
call foobar(dat(4:),2) ! OK since slice has 2 positions

call foobar(dat,2)     ! ?? smaller size
call foobar(dat(4),2)  ! ?? giving only start position

end program main
1 Like

These are all conforming.

2 Likes

DAT has 5 elements. DAT(4) is the 4th element, and there is only one following element, DAT(5), in array element order. Thus, it is possible to associate up to 2 elements of a dummy array. And this is what you do, by specifying the second argument as 2. This is covered as “sequence association” in the Standard, and it applies when the dummy array is explicit-shape (your example) or assumed-size.

1 Like