Sourced allocation from transposed array

MSa*_*Sap 5 fortran gfortran

I'm trying to understand a bug that I encountered in my code. When trying to allocate an array using the transpose of an array, I use an allocate statement with source=transpose(original_array) in my code. However, I don't get the expected result using this method. It seems that the indexing is off by one and the first row of the source array is skipped.

Example:

program testalloc
    real*8, allocatable :: a(:, :)
    real*8, allocatable :: b(:, :)

    allocate(b(2, 3))
    b(1, :) = [1, 2, 3]
    b(2, :) = [4, 5, 6]
    call printmat(b)

    a = transpose(b)
    call printmat(a) ! Good

    deallocate(a)
    allocate(a(3, 2), source=transpose(b))
    call printmat(a) ! Bad

    deallocate(a)
    allocate(a(3, 2))
    a = transpose(b) 
    call printmat(a) ! Good

contains

    subroutine printmat(mat)
        real*8, intent(in) :: mat(:, :)
        integer :: i

        write(*,*) 'print'
        do i = 1, size(mat, 1)
            write(*,*) mat(i, :)
        end do
    end subroutine

end program
Run Code Online (Sandbox Code Playgroud)

which gives

 print
   5.0000000000000000        3.0000000000000000     
   6.0000000000000000        0.0000000000000000     
   3.2114266979681025E-322   5.0000000000000000  
Run Code Online (Sandbox Code Playgroud)

for the sourced allocation after compiling with gfortran (gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04)) instead of the transposed original array. Am I doing something wrong here or is it a compiler bug?

fra*_*lus 2

来源分配

allocate(a(3, 2), source=transpose(b))
Run Code Online (Sandbox Code Playgroud)

是有效的。指定的形状a与源表达式的形状相同transpose(b)。结果,a取给定表达式的值。

编译器给出不同的结果是不正确的。这里不应该归咎于输出例程。

gfortran 8 似乎给出了预期的输出。

b有趣的是,对于 gfortran 7,如果本身不可分配,则会出现预期结果。