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

CVF pointer question

rahzan
New Contributor I
1,323 Views
! I am finding that if a cvf pointer points to an integer
! array the derefernced value is always zero!
! this is not the case with Real arrays
! what am I missing?

!===========
pointer (ptr_,pv_)
integer(4), allocatable:: allPtrs(:)

type tester
sequence
real(4), allocatable:: rPart1(:)
real(4), allocatable:: rPart2(:)
integer(4), allocatable:: iPart1(:)
end type tester
type(tester),static,save:: sample

!allocate memory
allocate(sample.rPart1(5)); sample.rPart1=0.
allocate(sample.rPart2(6)); sample.rPart2=0.;
allocate(sample.iPart1(7)); sample.iPart1=0 ;

allocate(allPtrs(3))
!store all pointers to array location
allPtrs(1)=loc(sample.rPart1) !
allPtrs(2)=loc(sample.rPart2)
allPtrs(3)=loc(sample.iPart1)

!(*)mini puzzle: despite the SEQUENCE the addresses are NOT sequential

!put stuff in the arrays
do i=1,5; sample.rPart1(i)=i+.1; enddo
do i=1,6; sample.rPart2(i)=i*2+.1; enddo
do i=1,7; sample.iPart1(i)=i*3; enddo

ptr_=allPtrs(1) !recall address
y=pv_ !get value, it is as calculated
y=sample.rPart1(1)

ptr_=allPtrs(3) !but on addressing the integer array
iy=int(pv_) !the data has "disappeared" and we get zero. (**)
iy=sample.iPart1(3) !though the array still has the values.

end
0 Kudos
1 Reply
Steven_L_Intel1
Employee
1,323 Views
1. The SEQUENCE means that the descriptors for the ALLOCATABLE arrays are stored in order - it has no bearing on the memory allocated for the allocatable components themselves.

2. The line:

iy=int(pv_)

doesn't do what you think it does. pv_ has the datatype REAL, so it is trying to interpret an integer value as if it were real. This generally results in a denormalized value, and the int() operation makes it zero. You probably want:

iy = transfer(pv_,iy)

instead.

Let me comment that what you are doing is creating pointer aliases. The compiler assumes you don't do this for ALLOCATABLE things, and you may get unexpected results with optimization. You're on your own.

Steve
0 Kudos
Reply