This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Bug in gfortran 4.2.3 for Mac


Hello,

  I believe I have run into a bug in gfortran which affects the Mac
version. The Linux version works as expected. The bug has to do with
the SAVE attribute of a derived data type with allocatable array
components. If the array is allocated and given values, its state
should be preserved in successive calls to the routine. However, in
the Mac version it gets deallocated upon return from the routine so
the next time it gets called it needs to be reallocated and its
contents may or may not have been preserved. I have prepared a short
test to illustrate what I mean:

Program simple_test
 Implicit None

  Print *,'First time'
  Call MyRoutine
  Print *,'Second time'
  Call MyRoutine

  Stop

  contains
    Subroutine MyRoutine
      Implicit None
      Type Object
         Real, Dimension(:), Allocatable :: Array
      End Type Object
      Integer :: i
      Type (Object), Save :: MyObject
      Logical, Save :: FirstTime=.True.

      Print *,'Is it the first time?',FirstTime
      Print *,'Is MyObject%Array allocated?',Allocated(MyObject%Array)

      If (.not. Allocated(MyObject%Array)) then
         Allocate(MyObject%Array(4))
      End if
      If (FirstTime) then
         Do i=1, 4
            MyObject%Array(i)=i
         End do
      End if
      FirstTime=.False.

      Print *,'MyObject%Array=',MyObject%Array
      Return
    End Subroutine MyRoutine

End Program simple_test

The output of this program on a Mac is:
 First time
 Is it the first time? T
 Is MyObject%Array allocated? F
 MyObject%Array=   1.000000       2.000000       3.000000       4.000000
 Second time
 Is it the first time? F
 Is MyObject%Array allocated? F
 MyObject%Array=   1.000000       2.000000       3.000000       4.000000

This is with gfortran 4.2.3, uname -a produces:
Darwin equipo 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36
PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

On a Linux PC it works as expected
If instead of a derived type I use a plain array it also works as expected:
Program simple_test
 Implicit None

  Print *,'First time'
  Call MyRoutine
  Print *,'Second time'
  Call MyRoutine

  Stop

  contains
    Subroutine MyRoutine
      Implicit None
      Real, Dimension(:), Allocatable, Save :: Array
      Integer :: i

      Print *,'Is Array allocated?',Allocated(Array)

      If (.not. Allocated(Array)) then
         Allocate(Array(5))
         Do i=1, 5
            Array(i)=i
         End do
      End if

      Print *,'Array=',Array
      Return
    End Subroutine MyRoutine

End Program simple_test

The output is:
 First time
 Is Array allocated? F
 Array=   1.000000       2.000000       3.000000       4.000000
5.000000
 Second time
 Is Array allocated? T
 Array=   1.000000       2.000000       3.000000       4.000000       5.000000

Should this be reported?

Thanks!

Hector
-- 
? ? ? ? ? ? ? ? ? ? Hector Socas-Navarro
? ? ? ? ? ?? Instituto de Astrofísica de Canarias
? Avda Vía Láctea S/N, La Laguna 38205, Tenerife (Spain)
Phone: (+34) 922-605748, Fax: (+34) 922-605210, hsocas@ll.iac.es
-----------------------------------------------------------------

Quidquid latine dictum sit, altum viditur


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]