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

pointer local variables

forall
Beginner
391 Views
quick question on pointers -

What happens to pointer local variables after the procedure is executed? eg

subroutine sub(a)
real,intent(in),target::a(:)
type myType
integer,pointer::i(:)=>null()
real,pointer::p(:)=>null()
endtype myType
type(myType)::t
... code cut
t%p=>a
... operations with t (passing it as arguments to other procedures, here as INTENT(IN) to avoid problems)
endsubroutine sub

after the code is executed, I am assuming "t" (and its componets) just disappears, with no effect on its target "a". Is this correct or should I nullify(t%p) for safety? I am chasing a memory leak and this was one of my (unlikely) suspicions.

Incidentally, what is the effect of making a variable such as "a" (especially a large array, possibly with many dimensions) a target, in terms of the code generation - does it disable any optimisations, or requires different storage mechanisms? Does it make a difference if the target is a dummy or local variable? Are there any runtime/memory penalties associated with having lots of targets? I am trying to get a better understanding of these 'memory management' / 'code structuring' things, so any info/guidance would be appreciated.

thanks,
dmitri

0 Kudos
1 Reply
Steven_L_Intel1
Employee
391 Views

When "t" goes out of scope, it becomes undefined. Nothing happens to the storage pointed to. If the components were ALLOCATABLE and not POINTER, then they would get deallocated automatically at routine exit (unless the variable was SAVEd.)

TARGET is advice to the compiler that can affect optimizations but has no effect on storage. If you need TARGET to satisfy language rules, then use it otherwise you may get incorrect results. If TARGET is not needed, then leave it off. TARGET tells the compiler that there can be a pointer to it. If a variable does not have the TARGET attribute, then the compiler can assume that all references to its storage are through the variable.

0 Kudos
Reply