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

pointers inside derived types during assignment

forall
Beginner
585 Views
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...
0 Kudos
3 Replies
Steven_L_Intel1
Employee
585 Views
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.
0 Kudos
forall
Beginner
585 Views
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
0 Kudos
Steven_L_Intel1
Employee
585 Views
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.
0 Kudos
Reply