Could you please examine the code below. 

The code constructs an array of structures, where the structure contains an
array of integers. The code then attempts to pass the first element of each
structure's array into an array in a subroutine - this step fails. It appears
as though the memory locations being accessed to generate the temporary array
in the subroutine has an incorrect stride. The stride appears to be 1, instead
of the size of the structure.

The code appears to run correctly when compiled with SGI and Cray compilers.

Results are:
------------
Results when compiled with gfc compiler:
Original:                     1          11          21
Original:                     2          12          22
Original:                     3          13          23
Original:                     4          14          24
Original:                     5          15          25

Added 100 to a(1):          101         102         103
Added 100 to a(1):          104         105          22
Added 100 to a(1):            3          13          23
Added 100 to a(1):            4          14          24
Added 100 to a(1):            5          15          25



Results when compiled with SGI compiler, or Cray compiler:
Original:            1,  11,  21
 Original:           2,  12,  22
 Original:           3,  13,  23
 Original:           4,  14,  24
 Original:           5,  15,  25

 Added 100 to a(1):  101,  11,  21
 Added 100 to a(1):  102,  12,  22
 Added 100 to a(1):  103,  13,  23
 Added 100 to a(1):  104,  14,  24
 Added 100 to a(1):  105,  15,  25

Code is:
--------

program  test_f90

    integer, parameter :: N = 5

    type test_type
        integer a(3)
    end type

    type (test_type) s(N)
    integer i

!   "s" is an array of structures,where the structure contains an array of
length 3
    do i=1, N
        s(i)%a(1) = i
        s(i)%a(2) = i + 10
        s(i)%a(3) = i + 20
    enddo

!   "s" is initialised as follows:  s(1)%a(1) =   1, s(1)%a(2) = 11,  s(1)%a(3)
= 21
!                                   s(2)%a(1) =   2, s(2)%a(2) = 12,  s(2)%a(3)
= 22
!                                   s(3)%a(1) =   3, s(3)%a(2) = 13,  s(3)%a(3)
= 23
!                                   s(4)%a(1) =   4, s(4)%a(2) = 14,  s(4)%a(3)
= 24
!                                   s(5)%a(1) =   5, s(5)%a(2) = 15,  s(5)%a(3)
= 25


    do i=1, N
    write(*, *) 'Original:          ', s(i)%a(1), s(i)%a(2), s(i)%a(3)
    enddo
    write(*, *) ' '

!   Add an offset to: s(1)%a(1), s(2)%a(1), s(3)%a(1), s(4)%a(1), s(5)%a(1)
    call test_sub(s%a(1), 100)

!   "s" should now contain:         s(1)%a(1) = 101, s(1)%a(2) = 11,  s(1)%a(3)
= 21
!                                   s(2)%a(1) = 102, s(2)%a(2) = 12,  s(2)%a(3)
= 22
!                                   s(3)%a(1) = 103, s(3)%a(2) = 13,  s(3)%a(3)
= 23
!                                   s(4)%a(1) = 104, s(4)%a(2) = 14,  s(4)%a(3)
= 24
!                                   s(5)%a(1) = 105, s(5)%a(2) = 15,  s(5)%a(3)
= 25

    do i=1, N
    write(*, *) 'Added 100 to a(1): ', s(i)%a(1), s(i)%a(2), s(i)%a(3)
    enddo

contains

subroutine test_sub(array, offset)
    integer array(:), offset
    integer i

    do i=1, N
        array(i) = i + offset
    enddo
end subroutine

end program


-- 
           Summary: error passing an array derived from struc ture elements
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: stephen dot jeffrey at nrm dot qld dot gov dot au


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

Reply via email to