Gfortran problem with elemental & operator overloading

Armelius Cameron armeliusc@gmail.com
Wed Jan 13 20:27:00 GMT 2010


Hello,

I have a rather peculiar problem that I am not sure the cause of. I hope I am
sending this is the appropriate list to discuss it, otherwise, I apologize and
please direct me to the right one.

I include a test program to illustrate the problem. This sample code is an
extract from a larger program, as I was trying to isolate the smallest parts
that still reproduce the error, therefore some of it would seem rather
contrived.

And here's the code:
=================================================
module UnitValue_Module

  implicit none
  private

  public :: &
    operator(*), &
    assignment(=)

  type, public :: UnitValue
    real :: &
      Value = 1.0
    character(31) :: &
      Label
  end type UnitValue

  interface operator(*)
    module procedure ProductReal_LV
  end interface operator(*)

  interface assignment(=)
    module procedure Assign_LV_Real
  end interface assignment(=)

contains

  elemental function ProductReal_LV(Multiplier, Multiplicand) result(P_R_LV)

    real, intent(in) :: &
      Multiplier
    type(UnitValue), intent(in) :: &
      Multiplicand
    type(UnitValue) :: &
      P_R_LV

    P_R_LV%Value = Multiplier * Multiplicand%Value
    P_R_LV%Label = Multiplicand%Label

  end function ProductReal_LV


  elemental subroutine Assign_LV_Real(LeftHandSide, RightHandSide)

    real, intent(inout) :: &
      LeftHandSide
    type(UnitValue), intent(in) :: &
      RightHandSide

    LeftHandSide = RightHandSide%Value

  end subroutine Assign_LV_Real

end module UnitValue_Module


!--------------------------------------------------

program TestProgram

  use UnitValue_Module

  implicit none

  type :: TableForm
    real, dimension(:,:), allocatable :: &
      RealData
  end type TableForm

  type(UnitValue) :: &
    CENTIMETER

  type(TableForm), pointer :: &
    Table

  allocate(Table)
  allocate(Table%RealData(10,5))

  Table%RealData(:,1) = Table%RealData(:,1) * CENTIMETER
  Table%RealData(:,2) = Table%RealData(:,2) * CENTIMETER
  Table%RealData(:,3) = Table%RealData(:,3) * CENTIMETER
  Table%RealData(:,5) = Table%RealData(:,5) * CENTIMETER

end program TestProgram

!-------------------------------------------------
================================================

With gfortran on my machine, the program give segmentation fault. Totalview
and gdb shows that the segfault happen on line 49, at the statement:

LeftHandSide = RightHandSide%Value

So far I found two workaround for it:
1. Change the character size for Label in the UnitValue derived type to
something else, eg. 7. However, the curious thing is that other value, e.g. 3,
30, 5, also produces the segmentation fault.

2. Not using pointer for the variable Table (and hence no need to
allocate(Table)) in the main program also seems to remove the segfault.

I can compile the code with Intel Fortran on my machine compiles and run it
without segfault.
And curiously, I tried this program on another machine with gfortran-4.3 and
it works fine there !

On my machine: I tried the following versions:
$ gfortran -v
<snip> gcc version 4.4.2 20091222 (Red Hat 4.4.2-20) (GCC)

$ /usr/src/gcc-4.3/bin/gfortran -v
<snip> gcc version 4.3.5 20100110 (prerelease) [gcc-4_3-branch revision
152405] (GCC)

$ /usr/local/gcc/latest/bin/gfortran -v
<snip>
gcc version 4.5.0 20091211 (experimental) (GCC)

The 4.4.2 version on my machines comes from Fedora 12.
I'd appreciate for any help / comment.

Thanks.
-- AC



More information about the Fortran mailing list