警告消息 (402):为参数临时创建的数组

Li-*_*uan 4 format io fortran fortran90 intel-fortran

我不断收到警告消息:

forrtl: warning (402): fort: (1): In call to I/O Read routine, an array temporary was created for argument #1
Run Code Online (Sandbox Code Playgroud)

当我运行以下代码时。我在论坛上翻阅了有关同一问题的旧帖子(请参阅此处),但我不太确定答案是否适用于我手头的问题。谁能帮我摆脱这个警告信息?谢谢。

program main
    implicit none
    integer :: ndim, nrow, ncol
    real, dimension(:,:), allocatable :: mat
    ndim = 13
    ncol = 1
    allocate( mat(ncol,ndim))
    call read_mat( mat, 'matrix.txt' )    
contains
    subroutine read_mat( mat, parafile)
    implicit none
    real, dimension(:,:), intent(out) :: mat
    character(len=*), intent(in) :: parafile
    character(len=500) :: par
    character(len=80) :: fname1
    integer :: n, m, pcnt, iostat
    m = size(mat,dim=2)
    n = size(mat,dim=1)
    mat = 0.
    open( unit= 10, file=parafile, status='old', action='read', iostat=iostat )
    if( iostat==0 )then
        pcnt = 1
        write(fname1,'(a,i3,a)'),'(',m,'(f10.5))' 
        do 
            read( unit=10, fmt=*, iostat=iostat ) mat(pcnt,:)
            pcnt = pcnt + 1
            if( pcnt > n ) exit 
        enddo
    else
        print*, 'The file does not exist.'
    endif
    close( 10 )
    end subroutine read_mat 
end
Run Code Online (Sandbox Code Playgroud)

Ian*_*anH 5

这与链接问题中的根本原因相同。引用指mat(pcnt,:)的是内存中不连续的数组元素存储,因此为了满足编译器对实际执行读取的运行时代码的内部要求,它会留出一些连续的存储,读入该存储,然后将元素复制出来临时存储到最终数组中。

\n\n

通常,在相对较慢的 IO 语句的上下文中,与临时相关的开销并不显着。

\n\n

防止警告的最简单方法是禁用相关的运行时诊断-check:noarg_temp_created或类似的。但这有点像大锤 \xe2\x80\x94 可能会创建您确实想了解的临时参数。

\n\n

另一种解决方法是使用 io-implied-do 显式指定要读取的元素。就像是:

\n\n
read (unit=10, fmt=*, iostat=iostat) (mat(pcnt,i),i=1,m)\n
Run Code Online (Sandbox Code Playgroud)\n\n

并带有适当的声明i

\n

  • 英特尔检查过于冗长,我建议禁用它们,我无法为参数列表中的每个数组构造函数发出警告。 (2认同)