Re: how can I use C allocated memory in fortran ?
- From: "James Van Buskirk" <not_valid@xxxxxxxxxxx>
- Date: Fri, 21 Mar 2008 09:57:33 -0600
"James Van Buskirk" <not_valid@xxxxxxxxxxx> wrote in message
news:PdydnQhsrsRMKX7anZ2dnUVZ_r-vnZ2d@xxxxxxxxxxxxxx
It's tricky to get the levels of indirection right
here, but I think I have done so.
Well, I had to test to be sure that I got it right. Here are tests
of both of my recommended methods:
C:\gfortran\clf\malloc_test>type malloc_test1.f90
subroutine get_in(x, n) bind(C,name='get_in')
use ISO_C_BINDING
implicit none
type(C_PTR), value :: x
integer(C_SIZE_T), value :: n
type(C_PTR), pointer :: rx
interface
function malloc(n) bind(C,name='malloc')
use ISO_C_BINDING
implicit none
type(C_PTR) malloc
integer(C_SIZE_T), value :: n
end function malloc
end interface
integer(C_SIZE_T) i
integer(C_INT), pointer :: tx(:)
call C_F_POINTER(x, rx)
rx = malloc(size(transfer(0_C_INT, [0_C_INT8_T]))*n)
call C_F_POINTER(rx, tx, [n])
tx = [(i,i=1,n)]
end subroutine get_in
program memtest
use ISO_C_BINDING
implicit none
interface
subroutine get_in(x, n) bind(C,name='get_in')
use ISO_C_BINDING
implicit none
type(C_PTR), value :: x
integer(C_SIZE_T), value :: n
end subroutine get_in
end interface
type(C_PTR) px
integer(C_INT), pointer :: x(:)
integer(C_SIZE_T) n
type(C_PTR), target :: tx
n = 10
px = C_LOC(tx)
call get_in(px, n)
call C_F_POINTER(tx, x, [n])
write(*,*) x
end program memtest
C:\gfortran\clf\malloc_test>C:\gcc_equation\bin\x86_64-pc-mingw32-gfortran
mallo
c_test1.f90 -omalloc_test1
C:\gfortran\clf\malloc_test>malloc_test1
1 2 3 4 5 6
7 8 9 10
C:\gfortran\clf\malloc_test>type malloc_test2.f90
subroutine get_in(x, n) bind(C,name='get_in')
use ISO_C_BINDING
implicit none
type(C_PTR), value :: x
integer(C_SIZE_T), value :: n
type(C_PTR), pointer :: rx
interface
function malloc(n) bind(C,name='malloc')
use ISO_C_BINDING
implicit none
type(C_PTR) malloc
integer(C_SIZE_T), value :: n
end function malloc
end interface
integer(C_SIZE_T) i
integer(C_INT), pointer :: tx(:)
call C_F_POINTER(x, rx)
rx = malloc(size(transfer(0_C_INT, [0_C_INT8_T]))*n)
call C_F_POINTER(rx, tx, [n])
tx = [(i,i=1,n)]
end subroutine get_in
program memtest
use ISO_C_BINDING
implicit none
interface
subroutine get_in(x, n) bind(C,name='get_in')
use ISO_C_BINDING
implicit none
type(C_PTR) :: x
integer(C_SIZE_T), value :: n
end subroutine get_in
end interface
type(C_PTR) px
integer(C_INT), pointer :: x(:)
integer(C_SIZE_T) n
n = 10
call get_in(px, n)
call C_F_POINTER(px, x, [n])
write(*,*) x
end program memtest
C:\gfortran\clf\malloc_test>C:\gcc_equation\bin\x86_64-pc-mingw32-gfortran
mallo
c_test2.f90 -omalloc_test2
C:\gfortran\clf\malloc_test>malloc_test2
1 2 3 4 5 6
7 8 9 10
Sweet success!
--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end
.
- Follow-Ups:
- Re: how can I use C allocated memory in fortran ?
- From: glen herrmannsfeldt
- Re: how can I use C allocated memory in fortran ?
- References:
- how can I use C allocated memory in fortran ?
- From: zhaopingsun
- Re: how can I use C allocated memory in fortran ?
- From: fj
- Re: how can I use C allocated memory in fortran ?
- From: James Van Buskirk
- how can I use C allocated memory in fortran ?
- Prev by Date: Available i/o unit function?
- Next by Date: Re: Real Symmetric Matrix Inversion
- Previous by thread: Re: how can I use C allocated memory in fortran ?
- Next by thread: Re: how can I use C allocated memory in fortran ?
- Index(es):
Relevant Pages
|
|