- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Add IMPLICIT NONE at the beginning of the subroutine, and you will see for yourself.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page