- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear all,
Here is a simplified case:
[fortran]program main type A end type A type, extends(A) :: B integer :: i = 0 end type B type(B), pointer :: p1 class(A), pointer :: p2 allocate(p1) p2 => p1 deallocate(p2) end program main[/fortran]I would like to release the memory allocated by "p1" through the handle "p2", but this case doesn't work.
How to accomplish it? Thanks!
dongli
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is a dangerous way of handling pointers: allocating through p1, associating p2 with p1 and then deallocating through p2 leaves p1 associated but dangling, i.e., pointing to freed memory and, therefore, unusable.
Try adding
if(associated(p1))write(*,*) ' p1 is still associated'
after line 15 and see what is printed.
Try adding
if(associated(p1))write(*,*) ' p1 is still associated'
after line 15 and see what is printed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear mecej4,
Actually, line 15 is not executed successfully at run time.
The error message is:
forrtl: severe (173): A pointer passed to DEALLOCATE points to an array that cannot be deallocated
dongli
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The action taken (if any) seems to depend on the compiler used. With this modification of your code:
[fortran]program mainIntel Fortran 12.0 says
type A
end type A
type, extends(A) :: B
integer :: i = 0
end type B
type(B), pointer :: p1
class(A), pointer :: p2
integer :: istat
allocate(p1,stat=istat)
if(istat.ne.0)write(*,*)' Istat 1 : ',istat
p2 => p1
deallocate(p2,stat=istat)
if(istat.ne.0)write(*,*)' Istat 2 : ',istat
if(associated(p1))write(*,*) ' p1 is still associated'
end program main
[/fortran]
[bash] Istat 2 : 173whereas Gfortran 4.5 prints only the second line. NAG Fortran 5.2 says:
p1 is still allocated
[/bash]
[bash]Runtime Error: ptr.f90, line 19: Dangling pointer P1 used as argument to intrinsic function ASSOCIATED
Target was DEALLOCATEd at line 17 of ptr.f90
Program terminated by fatal error
ptr.f90, line 19: Error occurred in MAIN[/bash]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
From your results, it seems that only NAG Fortran 5.2 gives the expected result!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
All of the compilers give the result expected by the standard, though I like NAG's the best. If you deallocate all or part of storage pointed to by a pointer, that pointer's association status becomes undefined and calling ASSOCIATED on it or using the pointer is not allowed by the standard - results are unpredictable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I think the "associated" intrinsic function should provide more information, such as the content of pointer p1 has been released by another pointer p2, then associated(p1) should tell me ".false.".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Associated cannot tell you that. It is your responsibility to dissassociate all pointers to object being deleted. Should that memory get re-allocated for something else, or a different object of the same type then a "pointing to allocated memory" indicator will produce a false positive (.false. for pointing to what you think it was).
Jim Dempsey
Jim Dempsey

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