Redian新闻
>
how to use function calloc of c in fortran?
avatar
how to use function calloc of c in fortran?# Unix - 噫吁兮,危乎高哉
s*l
1
want to use calloc in c to allocate memory dynamically in fortran,
because f77 can not support allocate memory dynamically.
I heard it is doable, who can help me and give me an example, like
allocate an array. Thanks.
avatar
s*s
2
in C:
....
float *B,...
B=(float *)malloc(N*sizeof(float));
fsub_(B,...)
in fortran:
subroutine fsub(B,....)
real B(1)

【在 s**l 的大作中提到】
: want to use calloc in c to allocate memory dynamically in fortran,
: because f77 can not support allocate memory dynamically.
: I heard it is doable, who can help me and give me an example, like
: allocate an array. Thanks.

avatar
s*l
3
Can it be more specified?
like an example which is tested. Thanks.
I tested the following
Fortran part:
program test
real B(1)
call fsub(B)
end
subroutine fsub(B)
real B(1)
do i=1,10
B(i)=i*1.
write(6,*) B(i)
enddo
end
C part:
#include
#include
void aaa( )
{
float * B;
B=(float *)malloc(10*sizeof(float));
fsub_(B);
}
But it says segmentation fault.....

【在 s**s 的大作中提到】
: in C:
: ....
: float *B,...
: B=(float *)malloc(N*sizeof(float));
: fsub_(B,...)
: in fortran:
: subroutine fsub(B,....)
: real B(1)

avatar
q*m
4
Do you want to call a "C" subroutine _from_ Fortran,
or do you want to call a Fortran subroutine _from_ C?

【在 s**l 的大作中提到】
: Can it be more specified?
: like an example which is tested. Thanks.
: I tested the following
: Fortran part:
: program test
: real B(1)
: call fsub(B)
: end
: subroutine fsub(B)
: real B(1)

avatar
s*l
5
I want to call a "C" subroutine from Fortran.

【在 q*****m 的大作中提到】
: Do you want to call a "C" subroutine _from_ Fortran,
: or do you want to call a Fortran subroutine _from_ C?

avatar
q*m
6
example
test.for:
program test
integer i, j
call sub(i,j)
print*, i, j
end
sub.c:
void sub_(int* i, int* j)
{
*i = 100;
*j=200;
return;
}
compile: gcc -c sub.c; g77 test.for sub.o

【在 s**l 的大作中提到】
: I want to call a "C" subroutine from Fortran.
avatar
s*l
7
Thanks, it works, but I need to allocate an array from C subroutine and
use it in fortran.

【在 q*****m 的大作中提到】
: example
: test.for:
: program test
: integer i, j
: call sub(i,j)
: print*, i, j
: end
: sub.c:
: void sub_(int* i, int* j)
: {

avatar
q*m
8
那没戏.
Suggest you make the Fortran program as subroutine, then call from C.

【在 s**l 的大作中提到】
: Thanks, it works, but I need to allocate an array from C subroutine and
: use it in fortran.

avatar
s*l
9
I heard it is possible.
I think the problem is the data communication between C and fortran.

【在 q*****m 的大作中提到】
: 那没戏.
: Suggest you make the Fortran program as subroutine, then call from C.

avatar
q*m
10
Ask the person whom you "heard" this from. Please share with us
how to do that once you work things out.

【在 s**l 的大作中提到】
: I heard it is possible.
: I think the problem is the data communication between C and fortran.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。