How to detect not initialized arrays

I have a very complex code and in order to catch a potential bug I am trying to compile the code so that I get a warning for real arrays that are (mistakenly) not initialized. I am using ifort/ifx on a Windows machine.

I managed to check for uninitialized real scalars but not arrays, which I find odd. Here is a MWE:

!##############################################################################
! PROGRAM MAIN
!##############################################################################

program main

    !use omp_lib
    implicit none

    write(*,*) "Calling subroutine_bad() which has uninitialized variables."

    call subroutine_bad()


    contains

    subroutine subroutine_bad()
        implicit none 
        real(8) :: x_bad_scal, x_bad_arr(2)

        write(*,*) "Inside sub: x_bad_arr  = ", x_bad_arr
        write(*,*) "Inside sub: x_bad_scal = ", x_bad_scal
    
    end subroutine subroutine_bad
    
end program main

I compile the code with ifort with these compilation flags:

/Od /debug:full /check:all /fpe:0 /traceback /warn:all /warn:interfaces /warn:unused /warn:uninitialized

This is the output:

Calling subroutine_bad() which has uninitialized variables.
 Inside sub: x_bad_arr  =   0.000000000000000E+000  0.000000000000000E+000
forrtl: severe (194): Run-Time Check Failure. The variable 'SUBROUTINE_BAD$X_BAD_SCAL$_1' is being used in 'C:\Users\aledi\Dropbox\1 - RESEARCH PROJECTS\7_Project_Leo_Chiara\SHARED_HAOMIN\Codes\4_Tests\test_uninit\main.f90(31,9)' without being defined
Image              PC                Routine            Line        Source
run_win.exe        00007FF67C961241  MAIN_ip_SUBROUTIN          31  main.f90
run_win.exe        00007FF67C9610BB  MAIN__                     21  main.f90
run_win.exe        00007FF67C9C61DB  Unknown               Unknown  Unknown
run_win.exe        00007FF67C9C6738  Unknown               Unknown  Unknown
KERNEL32.DLL       00007FFB519C7374  Unknown               Unknown  Unknown
ntdll.dll          00007FFB51FBCC91  Unknown               Unknown  Unknown
NMAKE : fatal error U1077: 'run_win.exe' : return code '0xc2'
Stop.

While the program correctly detect that x_bad_scal is NOT initialized, it fails to do the same for x_bad_arr

Can someone help me with this issue? I would like to make sure that I get a runtime error when using uninitialized arrays. As a second best, I would like to make sure that all uninitialized arrays are set to some easily detectable value, such as NaN, 1 million :slight_smile:

Thanks!

You can try -init=snan,arrays -fpe0 -g -traceback options.

1 Like

You might be able to initialise the arrays using -Qinit:snan,arrays

I tried it with your sample program and it works fine:

 Calling subroutine_bad() which has uninitialized variables.
 Inside sub: x_bad_arr  =                      NaN                     NaN
 Inside sub: x_bad_scal =                      NaN
1 Like

It was just a guess that this concatenation of keywords would work. The help output was not explicit about this possibility :slight_smile:

1 Like

Great, thanks! I missed the ,arrays