linux – Gfortran警告抱怨“Wmaybe -ininitialized”

我最近开发了一个相当长的Fortran代码.我正在使用的编译器是在Opensuse 13.1(64位)上的gfortran 4.8.1.但是当我使用-O2或-O3选项编译代码时,我收到很多关于“-Wmaybe -ininitialized”的警告.我设法将代码减少到最小的工作示例,如下所示.

在main.f90中

program main
    use modTest
    implicit none

    real(kind = 8), dimension(:, :), allocatable :: output
    real(kind = 8), dimension(:, :, :), allocatable :: input

    allocate(input(22, 33, 20), output(22, 33))
    input = 2.0
    call test(input, output)

end program main

在test.f90中

module modTest
contains
subroutine test(inputValue, outValue)
    use modGlobal
    implicit none

    real(kind = 8), dimension(:, :, :), intent(in) :: inputValue
    real(kind = 8), dimension(:, :), intent(out) :: outValue
    integer :: nR, nX, nM, iM, ALLOCATESTATUS
    real, dimension(:, :, :), allocatable :: cosMPhi

    nR = size(inputValue, 1)
    nX = size(inputValue, 2)
    nM = size(inputValue, 3) - 1
    allocate(cosMPhi(nR, nX, 0:nM), stat=ALLOCATESTATUS)
    call checkStatus(ALLOCATESTATUS)
    do iM = 0, nM
        cosMPhi(:, :, iM) = cos(iM * 1.0)
    end do
    outValue =  sum(inputValue * cosMPhi, 3)

end subroutine
end module

在global.f90中

module modGlobal
contains
    subroutine checkStatus(stat)
        implicit none
        integer, intent(in) :: stat
        if(stat /= 0) then
           print *, "allocation failed"
           stop
        end if
   end subroutine
end module

使用gfortran -O2 -Wall test.f90 main.f90 -o run编译时,会出现以下警告:

test.f90: In function 'test':
test.f90:9:0: warning: 'cosmphi.dim[2].stride' may be used uninitialized in this function [-Wmaybe-uninitialized]
     real, dimension(:, :, :), allocatable :: cosMPhi
^
test.f90:9:0: warning: 'cosmphi.dim[1].ubound' may be used uninitialized in this function [-Wmaybe-uninitialized]
test.f90:9:0: warning: 'cosmphi.dim[1].stride' may be used uninitialized in this function [-Wmaybe-uninitialized]
test.f90:9:0: warning: 'cosmphi.dim[0].ubound' may be used uninitialized in this function [-Wmaybe-uninitialized]
test.f90:9:0: warning: 'cosmphi.offset' may be used uninitialized in this function [-Wmaybe-uninitialized]

虽然我试图谷歌这个问题一段时间,但我仍然没有找到一个好的答案.一些相关网站是:
(1)https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58410
(2)https://groups.google.com/forum/m/#!topic/comp.lang.fortran/RRYoulcSR1k
(3)GCC -Wuninitialized / -Wmaybe-uninitialized issues

我使用gfortran 4.8.5测试了示例代码,警告仍然存在.是因为我在代码中做错了吗?任何帮助将不胜感激.提前致谢!

解决方法:

这是因为你在cosMphi的分配中使用stat = ALLOCATESTATUS,但之后不检查状态变量的值.只是省略.然后,如果分配失败,程序将崩溃 – 这是简单的方法,除非您需要更强大/更复杂的响应.

上一篇:使用f2py将numpy字符串格式数组传递给fortran


下一篇:python – f2py函数发布GIL