- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear all,
I want to delete a double linked list, but I observed that the element is not really deleted. So I made things simplified by the following example:
[fortran]program main
implicit none
integer, pointer :: a, b
allocate(a)
a = 1
b => a
deallocate(b)
print *, a
end program main[/fortran] The result is that "deallocate(b)" does not cause the memory release of "a", which is not what I expect.dongli
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a points to memory returned by b. Release of a memory block referenced by multiple pointers does not alter the state of the multiple pointers. It modifies the state of what the pointers point to. You will continue to see the last value written into this memory block (by the broken pointers) until this memory is overwritten.
Jim Dempsey
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It may help conceptually to think of
allocate ()
as equivalent to
allocate()
Pointer_var => anonymous_variable
The DEALLOCATE statement removes the association; since the anonymous_variable is no longer accessible in Fortran (since it does not have a name) code that references it is not valid and the compiler is free to make any suitable choice with regard to what is to be done in the compiled code.
In the interests of efficiency, many things, especially I/O, are done asynchronously, e.g., by buffering. If the compiler infers from your code that the subsequent behavior of the program is unaffected by some action, it may choose to produce code that avoids that action altogether or defer it to program termination, in the interests of efficiency.
allocate (
as equivalent to
allocate(
Pointer_var => anonymous_variable
The DEALLOCATE statement removes the association; since the anonymous_variable is no longer accessible in Fortran (since it does not have a name) code that references it is not valid and the compiler is free to make any suitable choice with regard to what is to be done in the compiled code.
In the interests of efficiency, many things, especially I/O, are done asynchronously, e.g., by buffering. If the compiler infers from your code that the subsequent behavior of the program is unaffected by some action, it may choose to produce code that avoids that action altogether or defer it to program termination, in the interests of efficiency.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, I see. I just need to maintain the association of a pointer, and leave the memory alone?
Cheers,
dongli
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