I have a a prrety large 1D array allocated. Let us say x(1:100000000). Then I want to use a 2D array pointer y(1:50000000,1:2) to point to x, so I can access array elements in a more meaningful way.
Is there an easy way to do this? I do not want to reallocate memory and copy x to y.
Thanks.
Is there an easy way to do this? I do not want to reallocate memory and copy x to y.
Thanks.
連結已複製
4 回應
Easy way - pass X(1) to a subroutine where the argument is declared (1:50000000,1:2) and do what you need there. This can also be done with pointer remapping in version 12 but I'll have to do some experiments first. You can also fake it by using C_F_POINTER(C_LOC(X),P,[50000000,2]) where P is a POINTER with dimension (:,:).
thanks Steve.
Few further questions:
Is the pointer remapping written in Fortran standard?
If I use C_F_POINTER to remap the pointer and later x becomes unaccessable, then can I deallocate memory with ponter Y?
Few further questions:
Is the pointer remapping written in Fortran standard?
If I use C_F_POINTER to remap the pointer and later x becomes unaccessable, then can I deallocate memory with ponter Y?
There are a few things that the program below does not test but it looks like pointer remapping is working in IVF 12 update 1.
Abhi
-----
Abhi
-----
[fxfortran] Program Test_PointerPartition
!
! Purpose: Test bounds remapping for pointers.
!
Implicit None
Real(8), Target :: A(0:20), C(22:26)
Real(8), Pointer :: B(:)
Integer, Target :: G(100)
Integer, Pointer :: H(:,:)
Integer :: i
!
!##########
!
A = 1.0d0; C = 0.0d0
Nullify(B)
B => A(11:20)
print *, LBOUND(B), UBOUND(B)
! correct answer: 1, 10
Nullify(B)
B => A
print *, LBOUND(B), UBOUND(B)
! correct answer: 0, 20
Nullify(B)
B => A(:)
print *, LBOUND(B), UBOUND(B)
! correct answer: 1, 21
Nullify(B)
B => C
print *, LBOUND(B), UBOUND(B)
! correct answer: 22, 26
Nullify(B)
B => C(:)
print *, LBOUND(B), UBOUND(B)
! correct answer: 1, 5
Nullify(B)
B(51:60) => A(11:20)
print *, LBOUND(B), UBOUND(B)
! correct answer: 51, 60
Nullify(B)
B(1:5) => C
print *, LBOUND(B), UBOUND(B)
! correct answer: 1, 5
Nullify(H)
H(0:24,2:5) => G
print *, LBOUND(H), UBOUND(H)
! correct answer: {0, 2}, {24,5}
do i=1,Size(G)
G(i) = i
end do
print *, H(5,:)
! correct answer: {6,31,56,81}
End Program Test_PointerPartition[/fxfortran]