Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple pointer problem

Status
Not open for further replies.

ya0037

Programmer
Oct 7, 2010
30
0
0
DE
Dears,

I have a strange problem which I do not understand what is it.
Based on pointers definition the simple code which I wrote below should work, but it does not please help me.

Code:
module all
contains
subroutine deall(a)
  integer, pointer :: a
  integer, pointer :: curr => null()
  curr => a
  if (associated(curr)) then
     deallocate(curr)
     nullify(curr)
  endif
end subroutine deall
end module

program test
  use all
  implicit none
  integer, pointer :: a=>null()
  integer, pointer :: b=>null()
  allocate(a)
  a=10
  print*,associated(a)
  call deall(a)
  print*,associated(a)
End Program test

the subroutine can not deallocate the pointer which according to text book should be the case
please help me in this regard.

cheers,
 
You've got what is known in Computer Science as a dangling reference. Basically whatever a was pointing to has been deallocated but a doesn't know about it. That is why languages like C# and Java have introduced reference counting: it is to avoid such problems (but you're not out of the woods - there are other problems)
Code:
subroutine deall(curr)
  integer, pointer :: curr
  integer, pointer :: dangling
  dangling => curr
  if (associated(curr)) then
     deallocate(curr)
     nullify(curr)
  endif
  print *, 'current  is ', associated(curr)
  print *, 'dangling is ', associated(dangling)
end subroutine deall
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top