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

How to release pointer?

Li_Dong
Beginner
820 Views
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
0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
820 Views
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
0 Kudos
mecej4
Honored Contributor III
820 Views
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.
0 Kudos
Li_Dong
Beginner
820 Views
Thanks, I see. I just need to maintain the association of a pointer, and leave the memory alone?
Cheers,
dongli
0 Kudos
Reply