I am recently developing a fairly long Fortran code. The compiler that I am using is gfortran 4.8.1 on Opensuse 13.1 (64-bit). However when I compiled the code with -O2 or -O3 options, I got many warnings about "-Wmaybe-uninitialized". I managed to reduce the code to a minimal working example as shown below.
In 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
In 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
In 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
When compiled using gfortran -O2 -Wall test.f90 main.f90 -o run
, the following warnings appear:
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]
Though I tried to google this issue for some time, I still failed to find a good answer. Some relevant websites are: (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
I tested the example code using gfortran 4.8.5 and the warnings still persist. Was it because I did anything wrong in the code? Any help would be greatly appreciated. Thanks in advance!