Unexpecting behaviour of allocating from assumed-shape array

Viewed 74

I have always assumed that if source is specified in the allocate statement, it will copy the whole array including the respective bounds. In the following code example, I assumed that the array x4 would have the same bounds as x2, namely -2 as the lower bound and 3 as the upper bound. Below, I listed the outputs of different compilers, which to my surprise are not consistent.

program test
    implicit none

    real :: x(-2:3)

    print *, lbound(x), ubound(x)

    call print_bounds(x,x)

contains

    subroutine print_bounds(x1,x2)
        
        real, intent(in) :: x1(:)
        real, intent(in) :: x2(-2:)

        real, allocatable :: x3(:)
        real, allocatable :: x4(:)

        allocate(x3, source = x1)
        allocate(x4, source = x2)

        print *, lbound(x1), ubound(x1)
        print *, lbound(x2), ubound(x2)
        print *, lbound(x3), ubound(x3)
        print *, lbound(x4), ubound(x4)

    end subroutine print_bounds

end program

Here is the output of gfortran 8 and ifort

          -2           3
           1           6
          -2           3
           1           6
          -2           3

On the other hand gfortran 9 and gfortran 10 leads to

          -2           3
           1           6
          -2           3
           1           6
           1           6

The section 6.7.1.2 in standard clearly states

When an ALLOCATE statement is executed for an array with no allocate-shape-spec-list, the bounds of source- expr determine the bounds of the array. Subsequent changes to the bounds of source-expr do not affect the array bounds.

Do gfortran 9 and gfortran 10 violate the standard with their unexpected behavior?

0 Answers
Related