Gfortran/ifx, reading null values from namelist

Hello,
I am trying to use the namelists to effectively probe if a variable <varname> belongs to a certain defined namelist, without specifying a value of the variable.

The idea is to write a string like line = &my_nml <varname> / and then trying to read it into nml as: read(line, nml=my_nml, iostat=ios, iomsg=msg). I expect the ios to be zero when <varname> is part of my_nml, and nonzero otherwise.

I found this works with gfortran, but for ifx i need to add an = sign after the <varname>, so the string looks something like line = &my_nml <varname> = /. So far so good.

Now switching back to gfortran, with the = sign after the variable name however, seems to cause a segmentation fault at the moment of reading the nml, for real, allocatable variables, but not for integer, allocatable.

I also tried to put two commas after the = sign but i get the same crash with gfortran.

Anybody have some idea what i’m doing wrong?

The full toy program where you can input the variable name in stdin, and print the ios and iomsg after trying to read that name from the nml is pasted below.

program main
  implicit none
  integer :: i
  integer, allocatable :: i1d(:), i2d(:,:)
  real :: r
  real, allocatable :: r1d(:), r2d(:,:)
  namelist/this_nml/i, i1d, i2d, r, r1d, r2d

  character(len=128) :: line, msg, inp
  integer :: ios


  do while( .true. )

     !! read from stdin
     read(*,"(a128)") inp

     if( trim(inp) == "exit" ) exit

     !! write nml format in line
     line = "&this_nml "//trim(inp)//"  / "
     write(*,*) "nml line is:", line

     !! read nml from line
     read(line, nml=this_nml, iostat=ios, iomsg=msg)
     write(*,*) "ios  :",ios
     if( ios /= 0 ) then
        write(*,*) "iomsg :",msg
     end if

  end do

end program main```

I am not sure whether namelist is a good fit for the goal here, but if it is to be used for some reason, we may need to allocate all allocatable variables beforehand…? E.g.:

  allocate( i1d(0), i2d(0,0), r1d(0), r2d(0,0) )
  !! I don't know appropriate sizes, so just for test

Yes. NAMELIST groups can only read an allocatable variable if it is allocated before the read. It is an interesting question as to what the standard says, if anything, about reading into a zero-size variable.

1 Like