加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 00:35:30 所属栏目:Linux 来源:网络整理
导读:我最近开发了一个相当长的Fortran代码.我正在使用的编译器是在Opensuse 13.1(64位)上的gfortran 4.8.1.但是当我使用-O2或-O3选项编译代码时,我收到很多关于“-Wmaybe -ininitialized”的警告.我设法将代码减少到最小的工作示例,如下所示. 在main.f90中 progr
我最近开发了一个相当长的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),:,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),intent(in) :: inputValue
    real(kind = 8),intent(out) :: outValue
    integer :: nR,nX,nM,iM,ALLOCATESTATUS
    real,allocatable :: cosMPhi

    nR = size(inputValue,1)
    nX = size(inputValue,2)
    nM = size(inputValue,3) - 1
    allocate(cosMPhi(nR,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,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,但之后不检查状态变量的值.只是省略.然后,如果分配失败,程序将崩溃 – 这是简单的方法,除非您需要更强大/更复杂的响应.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读