Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

pointer dummy argument

Blane_J_
New Contributor I
554 Views
Hi to everyone. I've found a problem illustrates as follows:
module mod

    type :: test
        integer :: i
    end type test

    contains

    subroutine equals(left, right)

        type(test) :: left
        type(test) :: right

        write(*,*) loc(p), loc(q)

    end subroutine equals
end module mod

program main
    use mod

    type(test), target  :: tar
    type(test), pointer :: p=>null(), q=>null()

    allocate(p, q)
    write(*,*) loc(p), loc(q) ! First
    p => tar
    q => tar
    write(*,*) loc(p), loc(q) ! Second
    call equals(p,q)          ! Third

end program main

The First and Second WRITE statements work fine and loc(p) and loc(q) equal to each other in the Second part. But in the Third time, the returned WRITE statement within the SUBROUTINE outputs a wired result which loc(p) and loc(q) were no longer identical. Is there anything wrong with the code? And if so, what are the returned values in the Third WRITE statement considering that they are not the location of pointer p and q which should be equal then?

0 Kudos
4 Replies
mecej4
Honored Contributor III
554 Views

Add IMPLICIT NONE at the beginning of the subroutine, and you will see for yourself.

0 Kudos
Blane_J_
New Contributor I
554 Views
Ha, I'am really sorry to miswrite left & right to p & q in the subroutine. Thanks mecej4, your suggestion is right, but it's not the point I want to figure out. Even so, when I replaced the keyword TYPE wih CLASS to declare the two dummy argument left & right, I got an wrong answer of different loc(left) and loc(right) in the Third part, none of the values are identical to loc(p) & loc(q) in the Second part. What's this problem?
0 Kudos
mecej4
Honored Contributor III
554 Views

Which version of the compiler did you use? I do not get the errors that you reported -- with IFort 15.0.2.

It may not matter in this small example, but there is a potential problem in the code: you allocated the two pointer variables, and then re-associated the pointers with something else. Once you do this, you have an irreparable memory leak.

0 Kudos
Blane_J_
New Contributor I
554 Views

I'm using Intel(R) Visual Fortran Compiler XE 13.0 Update 1 for Windows*. So is it to say that the difference between two versions of compilers caused the result? By the way, I awared of the memory leak problem, thanks mecej4. And can anyone suggest a safe way to avoid it when declaring and initializing pointers?

0 Kudos
Reply