4

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!

Community
  • 1
  • 1
Ben
  • 75
  • 1
  • 5
  • Thanks to the comment of @M.S.B., the original question was found not being able to describe the problem properly. The current improved question should be a proper one. – Ben Dec 13 '15 at 19:22

2 Answers2

4

Its because you use stat=ALLOCATESTATUS in the allocation of cosMphi but don't check the value of the status variable afterwards. Just omit that. Then if the allocation fails, the program will crash -- that's the easy way, unless you need a more robust/sophisticated response.

M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • Thank you very much for your comment. The warnings indeed disappear when I refer to ALLOCATESTATUSE after allocation. However in my original code I did check it, and the minimal example is a bit over-simplified. Sorry. In my original code however, I used a global function to check the status, i.e. `call checkStatus(ALLOCATESTATUS)`. This global function sits in a global module and is just a very simple `if(ALLOCATESTATUS /=0)` block. I tested the above minimal example by checking status this way, and the warnings still appear. Is it a bad practice to move the checking code to a global module? – Ben Dec 12 '15 at 11:57
  • I agree that the gfortran error messages were not very clear about the actual problem. Maybe the compiler isn't diagnosing it well, and is mis-diagnosing when you use a function to check the status variable. – M. S. B. Dec 12 '15 at 16:09
  • I suppose so. Maybe this could be a potential bug of gfortran. Thanks for your help anyway! – Ben Dec 13 '15 at 18:55
1

Agree with the answer by M. S. B. and the warning message is a bit unclear. Here is what NAG Fortran compiler has to say:

nagfor -kind=byte -c test.f90
NAG Fortran Compiler Release 6.0(Hibiya) Build 1057
Questionable: test.f90, line 20: Variable ALLOCATESTATUS set but never referenced
[NAG Fortran Compiler normal termination, 1 warning]
macelee
  • 321
  • 2
  • 7