- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
1 Reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

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