Search code examples
fortrangfortranallocatable-array

Unable to print allocated status of a allocatable inside a derived type


I would like to know why this code returns error in the last print.

With gfortran 7.4.0 fails but with ifort 18.0.3 works well.

program test
implicit none
type :: syntax
  integer, allocatable :: f(:)
end type
type(syntax), allocatable :: rhs(:)

allocate(rhs(2))
print*, allocated(rhs(2)%f)
print*, allocated(rhs(size(rhs))%f)
end program

The gfortran error is:

 F

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7f4cf40442da in ???
#1  0x7f4cf4043503 in ???
#2  0x7f4cf3c76f1f in ???
#3  0x55aa522e5e50 in test
    at /home/pena/Escritorio/c.f90:10
#4  0x55aa522e5f0d in main
    at /home/pena/Escritorio/c.f90:11
Violación de segmento (`core' generado)

Solution

  • This is a bug in gfortran which is not present in version 8.

    If you can't upgrade your compiler, then there is an easy alternative: simply use a temporary variable for size(rhs):

    hack = SIZE(rhs)
    print*, allocated(rhs(hack)%f)
    

    giving output with gfortran 7.4.0:

     F
     F