When using the transfer intrinsic to convert a character string to an array of
integers, and the string does not fit exactly into the integer array data
block, the resultant integer array is scope dependent, with the same code
giving a different resultant integer array in different subroutines/program
units. Basically, the transfer function is not deterministic in the case where
the source data is smaller than the destination data.

There is a small program below to demonstrate the issue. The integer array is
printed twice, and (on my machine) is different each time. The code producing
the two sets of output is exactly the same, but in the first case the code is
embedded in the main program, and in the other case in a subroutine.

I do not have a copy of the standard, but I cannot imagine that this behavior
is correct. The problem arises even if the destination array is initialized to
zero (as shown in the example).


module DataMod
   type Byte
      character(len=1) :: singleByte
   end type
end module

subroutine sub()
   use DataMod
   integer :: i(1), intarray(4), j
   character(len=15) :: str1
   type (Byte) :: bytes(15)
   type (Byte) :: byteProt(1)

   bytes = transfer('123456789012345', byteProt)
   print *, bytes(:)%singleByte
   intarray = 0
   intarray = transfer(bytes, i)
   print *, intarray

end subroutine

program test
   use DataMod
   integer :: i(1), intarray(4), j
   character(len=15) :: str1
   type (Byte) :: bytes(15)
   type (Byte) :: byteProt(1)

   bytes = transfer('123456789012345', byteProt)
   print *, bytes(:)%singleByte
   intarray = 0
   intarray = transfer(bytes, i)
   print *, intarray

   call sub()

end program


-- 
           Summary: TRANSFER intrinsic is context sensitive
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: drewmccormack at mac dot com
 GCC build triplet: gcc version 4.3.0 20070511 (experimental)
  GCC host triplet: powerpc-apple-darwin8.9.0
GCC target triplet: powerpc-apple-darwin8.9.0


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

Reply via email to