- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
what happens to pointers during assignment of derived types? eg, given definition
!----
type gtype
integer::i=0
real,pointer::p(:)=>null()
endtype gtype
type(gtype)::v1,v2
!-----
will the following
case A:
v1%i=5;allocate(v1%p(10))
v2=v1
result in v2%p being dereferenced to the same unnamed storage area as v1%p? (as well as the value of "i" being copied across)?
I guess this kind of code is a great way to create a memory leak...
!----
type gtype
integer::i=0
real,pointer::p(:)=>null()
endtype gtype
type(gtype)::v1,v2
!-----
will the following
case A:
v1%i=5;allocate(v1%p(10))
v2=v1
result in v2%p being dereferenced to the same unnamed storage area as v1%p? (as well as the value of "i" being copied across)?
I guess this kind of code is a great way to create a memory leak...
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, indeedy, it will. This is why TR15581 came into being, allowing ALLOCATABLE components and making assignment of derived types containing such components "do the right thing". Make p an allocatable instead of a pointer, and you'll get the behavior you want, with v2%p being an independent copy of whatever was in v1%p.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
thanks Steve - at this point I may actually neeed both behaviours (for different variables, of course).
Also, just to make sure I understand whats going on:
caseB:
v1%i=5 ! (ie, with unassociated v1%p)
v2=v1
should leave v2%p unassociated
caseC:
v1%i=5 ! (ie, with unassociated v1%p)
v2%p=>sometarget
v2=v1
should copy i but nullify v2%p
Also, just to make sure I understand whats going on:
caseB:
v1%i=5 ! (ie, with unassociated v1%p)
v2=v1
should leave v2%p unassociated
caseC:
v1%i=5 ! (ie, with unassociated v1%p)
v2%p=>sometarget
v2=v1
should copy i but nullify v2%p
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is p pointer or allocatable? If allocatable, case C is illegal. I believe you are correct on case B.
The semantics are: deallocate the left-hand-side,then allocate the left-hand-side to match the right and copy the data. If right side is not allocated, then the allocate/copy doesn't happen.
The semantics are: deallocate the left-hand-side,then allocate the left-hand-side to match the right and copy the data. If right side is not allocated, then the allocate/copy doesn't happen.

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