Derived-type initialization (was: Re: Again on gfortran for Mac OS-X)

Paolo Giannozzi p.giannozzi@fisica.uniud.it
Sun Jan 6 18:15:00 GMT 2008


On Jan 5, 2008, at 22:17 , Paul Thomas wrote:

> I'll commit it as obvious tomorrow night, together with testcase  
> and ChangeLog.


Thank you, and thanks to all the nice people of the gfortran mailing  
list for the
quick fix. When I submitted a bug report to a commercial f90 compiler  
(one
produced by a very big name) it took 6 months to be fixed.

Maybe the following is the same problem you just fixed, but maybe it is
a different problem (or maybe my code is plain wrong): it looks like a
derived-type variable, containing an allocatable array and a variable  
set
to an initial value, picks some random number instead of the initial  
value,
unless it has the "save" attribute. Again, 31-dec gfortran version on  
Mac
OS-X. Only difference between "mah0" and "mah1": the "save" attribute

program boh
   !
   call mah0
   call mah1
   !
end program boh
!
subroutine mah0
   !
   type mix_type
      real(8), allocatable :: a(:)
      integer :: n=1023
   end type mix_type
   type(mix_type) :: t
   !
   allocate(t%a(1))
   t%a=3.1415926
   print *, 'n,a=',t%n,t%a
   deallocate(t%a)
   !
end subroutine mah0
!
subroutine mah1
   !
   type mix_type
      real(8), allocatable :: a(:)
      integer :: n=1023
   end type mix_type
   type(mix_type), save :: t
   !
   allocate(t%a(1))
   t%a=3.1415926
   print *, 'n,a=',t%n,t%a
   deallocate(t%a)
   !
end subroutine mah1

$ gfortran  boh.f90
$ ./a.out
  n,a=     2123354   3.1415925025939941
  n,a=        1023   3.1415925025939941

$ gfortran -O3  boh.f90
$ ./a.out
  n,a=          -1   3.1415925025939941
  n,a=        1023   3.1415925025939941

No problem if "a" is a fixed-dimension array

Paolo
---
Paolo Giannozzi, Dept of Physics, University of Udine
via delle Scienze 208, 33100 Udine, Italy
Phone +39-0432-558216, fax +39-0432-558222



More information about the Fortran mailing list