linux – Gfortran警告抱怨“Wmaybe -ininitialized”
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – Gfortran警告抱怨“Wmaybe -ininitialized”,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2487字,纯文字阅读大概需要4分钟。
内容图文
![linux – Gfortran警告抱怨“Wmaybe -ininitialized”](/upload/InfoBanner/zyjiaocheng/969/393ef03712164320b01f9eef9adb89c3.jpg)
我最近开发了一个相当长的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,但之后不检查状态变量的值.只是省略.然后,如果分配失败,程序将崩溃 – 这是简单的方法,除非您需要更强大/更复杂的响应.
内容总结
以上是互联网集市为您收集整理的linux – Gfortran警告抱怨“Wmaybe -ininitialized”全部内容,希望文章能够帮你解决linux – Gfortran警告抱怨“Wmaybe -ininitialized”所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。