I want to keep a deepcopy of the resulting fortran pointers from c_f_pointer
for check purpose and so on.
(EDIT 1: I used 'deepcopy' as 'making a duplicate which is independent from its original'.
a = (/1,2,3,4/)
a_deepcopy = a
a_deepcopy(2) = 1
In above example, the original a
remains (/1,2,3,4/)
while a_deepcopy
is changed to (/1,1,3,4/)
. If it was not a deepcopy, changing a_deepcopy
's element would also change it's original a
's element. I used the word 'deep copy' just becuase I've seen this kind of naming in Python context.)
program test1
use ...
use iso_c_binding
implicit none
...
type(c_ptr) :: rows_start_c, rows_end_c, col_indx_c, values_c
integer,pointer :: rows_start_f(:), rows_end_f(:), col_indx_f(:)
real*8 ,pointer :: values_f(:)
...
(*) call c_f_pointer(rows_start_c, rows_start_f, [DIM_r])
call c_f_pointer(rows_end_c , rows_end_f , [DIM_c])
call c_f_pointer(col_indx_c , col_indx_f , [rows_end_f(DIM_r)-1])
call c_f_pointer(values_c , values_f , [rows_end_f(DIM_r)-1])
...
end program test1
I've googled fairly much to find examples for the deepcopy of fortran pointer
to allocatable
array, which is the subject that somebody would have questioned already, but couldn't find a proper one.
The following code to copy a pointer
to allocatable
compiles and works fine so that deepcopy of 'pointer P' is generated in 'allocatable A2' array, but from the answers on my previous questions, I get to know that I'd better be careful treating pointer
and allocatable
in some mixed way even though they seems to work fine.
program pointer3
implicit none
real*8,allocatable :: A2(:)
real*8,target :: A(4)
real*8,pointer :: P(:)
A = 3.1416d0
P => A
allocate ( A2(size(A,1)) )
A2 = P # Assign 'pointer' to 'allocatable'
print *, 'Initially'
write(*,'(a, 4f6.2)') 'A ', A
write(*,'(a, 4f6.2)') 'P ', P
write(*,'(a, 4f6.2)') 'A2', A2
P(2) = 1.d0
print *, 'After'
write(*,'(a, 4f6.2)') 'A ', A
write(*,'(a, 4f6.2)') 'P ', P
write(*,'(a, 4f6.2)') 'A2', A2
end program
Initially
A 3.14 3.14 3.14 3.14
P 3.14 3.14 3.14 3.14
A2 3.14 3.14 3.14 3.14
After
A 3.14 1.00 3.14 3.14
P 3.14 1.00 3.14 3.14
A2 3.14 3.14 3.14 3.14 # A2 didn't changed after pointer modification, so a deepcopy seems to be generated
So the questions goes,
Is the above code right practice of deepcopying a
pointer
into aallocatable
array?If I were with ordinary fortran
pointer
and itstarget
, I would try to deepcopy it toallocatable
arrays just assigning itstarget
attribute into theallocatable
like below (Even though I can't do this since I'm withc_f_pointer
and the equivalenttarget
isc_ptr
, which is not a fortran array).
program pointer3
implicit none
real*8,allocatable :: A2(:)
real*8,target :: A(4)
real*8,pointer :: P(:)
A = 3.1416d0
P => A
!allocate ( A2(size(A,1)) ) # Commenting this line does not induce a compile error, as well as a runtime error.
A2 = A # Assign 'target' to allocatable
print *, 'Initially'
write(*,'(a, 4f6.2)') 'A ', A
write(*,'(a, 4f6.2)') 'P ', P
write(*,'(a, 4f6.2)') 'A2', A2
P(2) = 1.d0
print *, 'After'
write(*,'(a, 4f6.2)') 'A ', A
write(*,'(a, 4f6.2)') 'P ', P
write(*,'(a, 4f6.2)') 'A2', A2
end program
This works fine as the first try of assigning a pointer right into allocatable array, but this seems weird because apparently 'A', and 'A2' differs on their type: real*8, allocatable
and real*8, target
.
I think both of above two methods to make deepcopy of a fortran pointer
are neither proper, nor safe ones. How can I make a deepcopy of pointer
in a safe, and robust way?
Thank you for reading this question.