I have a derived type with an allocatable array and have overloaded some
operations. Unfortunately assignments like a=-a don't work.

Code in the Module, where I definde the type:

  type alltype
     double precision :: a
     double precision,allocatable :: b(:)
  end type

      interface assignment(=)
      module procedure at_from_at
      end interface

      interface operator(-)
      module procedure  neg_at
      end interface

  contains

    subroutine at_from_at(b,a)
      type(alltype), intent(in) :: a
      type(alltype), intent(out) :: b

      b%a=a%a
      allocate(b%b(2))
      b%b=a%b

    end subroutine at_from_at

    function neg_at(a) result(b)
      type(alltype), intent(in) :: a
      type(alltype) :: b

      b%a=-a%a
      allocate(b%b(2))
      b%b=-a%b

    end function neg_at

The allocatable array has an dimension of 2 in this example...

Now the code:

 use typemodule

 type(alltype) t1,t2,t3

 allocate(t1%b(2))
 t1%a=0.5d0
 t1%b(1)=1d0
 t1%b(2)=2d0


 t2=-t1
 write(*,*) t2%a, t2%b

 t1=-t1
 write(*,*) t1%a, t1%b

Prints: 
 -0.50000000000000000      -1.00000000000000000       -2.0000000000000000
Segmentation fault

It seems that in the Module, the allocatated array of t1 doesn't get passed
properly in the t1=-t1 part. I have also seg. faults in situations like a=a+b
(where + is overloaded simiarly)

The same code works correctly with ifort.


-- 
           Summary: Overloading problems with derived type with allocatable
                    array
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: everyo at gmx dot net
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35824

Reply via email to