linux - Gfortran 警告提示 "Wmaybe-uninitialized"

标签 linux gcc fortran gfortran

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

在 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

在测试.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 但之后没有检查状态变量的值。只是省略那个。然后,如果分配失败,程序就会崩溃——这是最简单的方法,除非您需要更强大/更复杂的响应。

关于linux - Gfortran 警告提示 "Wmaybe-uninitialized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34233374/

相关文章:

c - 为什么 gcc4 不展开这个循环?

loops - 不确定在 openmp 循环中应该共享或私有(private)什么

fortran - forall索引关联实体?

linux - NPTL 默认堆栈大小问题

linux - udp 从驱动程序发送

c++ - GCC: template previously defined 错误

file-io - Fortran90+ 中具有可分配组件的数据类型的二进制读/写

linux - 有条件地添加或附加到 linux 脚本中的文件

c - 为什么 gcc 讨厌我的简单 makefile?

c++ - 使用备用编译器构建 Travis-CI R 项目