On Mar 3, 2014, at 12:41 PM, Tom Clune <[log in to unmask]> wrote:
>
> I have attached below a very simple code which attempts to store a pointer inside a data structure that points to an individual element of an array. I get the expected/desired results "1" on the final print statement from 2 compilers, but with a third I get the value "0" on the final print. The vendor in question has responded that my code is not standard conforming, which I'm more than prepared to concede. Unfortunately they did not provide any details, so I don't know if it is the overload of the assignment operator or something more mundane.
>
> Could someone (A) explain why it is not standard conforming and/or (B) give advice on how I might go about doing something equivalent?
>
I’m not seeing why the code fails to be conforming. On the other hand, I’m also not seeing the point of going to such contortions to simulate support for vectors when this is already built into the language. Here is my proposal for (B):
program main
implicit none
integer,allocatable :: v(:), array(:)
array = [1]
v = array
print*,"Should be 1 after return: ", v
end program main
Cheers,
Bill
> Thanks
>
> - Tom
>
>
> module Foo_mod
> implicit none
>
> type Foo
> integer :: value
> end type Foo
>
> end module Foo_mod
>
> module Vector_mod
> use Foo_mod
> implicit none
>
> type Element
> type (Foo), pointer :: item => null()
> end type Element
>
> type Vector
> integer :: numElements
> type (Element), allocatable :: elements(:)
> contains
> procedure :: copyFromArray
> generic :: assignment(=) => copyFromArray
> end type Vector
>
> interface Vector
> module procedure newVector
> end interface Vector
>
> contains
>
> function newVector() result(v)
> type (Vector) :: v
> allocate(v%elements(0))
> v%numElements = 0
> end function newVector
>
>
> subroutine copyFromArray(this, array)
> class (Vector), intent(inout) :: this
> type (Foo), target, intent(in) :: array(:)
>
> integer :: i, n
>
> n = size(array)
>
> if (allocated(this%elements)) deallocate(this%elements)
> allocate(this%elements(n))
>
> do i = 1, n
> this%elements(i)%item => array(i)
> end do
>
> print*,'appears to be 1 before return: ', this%elements(1)%item%value
>
> end subroutine copyFromArray
>
>
> end module Vector_mod
>
> program main
> use Foo_mod
> use Vector_mod
> implicit none
>
> type (Vector) :: v
> type (Foo), target, allocatable :: array(:)
>
>
> array = [Foo(1)]
> v = array
>
> print*,"Should be 1 after return: ", v%elements(1)%item%value
>
> end program main
>
>
>
Bill Long [log in to unmask]
Fortran Technical Suport & voice: 651-605-9024
Bioinformatics Software Development fax: 651-605-9142
Cray Inc./ Cray Plaza, Suite 210/ 380 Jackson St./ St. Paul, MN 55101
|