2

I am using:

  open(iunit,file=dexfile,status='old',iostat=status)
   if(status /= 0)then
     write(*,*) 'Unable to open dex file'
   endif

in Geany, but get this error:

Warning: 'iunit' is used uninitialized in this function [-Wuninitialized]
Ashley Ly
  • 21
  • 3

3 Answers3

2

If your compiler has this option, you can use the newunit command in function open to return a flag to an available unit ID upon opening:

open(newunit=iunit,file=dexfile,status='old',iostat=status)

In this case, the open command will return a value of iunit as output to that call, instead of input.

Otherwise, you could cook a snippet that does that for you, for example:

! Find a logical unit which is not currently in use
integer function first_available_unit() result(iunit)
   logical :: is_open
   
   do iunit=10,999 ! Skip units 1-10 as they're sometimes system reserved
      inquire(unit=iunit,opened=is_open)
      if (.not.is_open) return
   end do
end function first_available_unit

and then run:

   iunit = first_available_unit()
   open(unit=iunit,file=dexfile,status='old',iostat=status)
Federico Perini
  • 1,414
  • 8
  • 13
1

Declare iunit something like this:

integer, parameter      :: iunit = 11

It doesn't have to be with the parameter but I normally do this because it doesnt have to change during execution and if I have multiple open files it is less error prone since it cannot be altered.

Erik Thysell
  • 1,160
  • 1
  • 14
  • 31
1

There are times in the life of a Fortran program where the value of a variable must be referenced/known: the variable must be defined. The warning message you get from the compiler is it reporting that it is confident you haven't given iunit a value. In general it isn't the compiler's job to check that you have defined all variables before referencing them, but when the compiler can check it may report so.

iunit in this case must be defined because its value is used to be the unit for a file connection. Once we know what the warning means we have two answers which suggest approaches to correct the identified issue:

  • be sure to check that you've given a value (and check for typos in variable names, such as by using implicit none)

  • use newunit=iunit instead of [unit=]iunit: here you don't need to specify a value, because the compiler chooses an appropriate value for you.

francescalus
  • 30,576
  • 16
  • 61
  • 96