N
NM
Hello All
I am writing some progam that involves both C++ and Fortran. Some of the
existing code is in Fortran. The main program will be in C++ and it will
call some Fortran subroutine. All the memory allocation has to be done in
C++.
For arrays of structures and arrays (including multi-dimensional array) of
primitive data types declared in Fortran, I know the corresponding data
structures in C++. So I can declare those data structures in C++, allocate
them, and pass them as argument to Fortran subroutine. But when a member of
the structure is a pointer (declared in Fortran), I cannot figure out what
should be the corresponding data structures in C++. I have tried pointer to
the same type in C++, but it do not work and the Fortran subroutine will get
a segmentation fault when accessing the pointer.
I cannot change the data structures defined in Fortran. What I can do is to
use appropiate data structures in C++ and pass it to the Fortran subroutine
as argument.
Can anyone tell me what should be the data structure defined in C++ that
corresponds to the Fortran structure "node". Thanks in advance.
Here is my example fortran code
subroutine test_fortran_sub(NODES,MAXNODS)
c
implicit none
type node
integer :: order
double precision,dimension),pointer :: zdofs
end type node
integer :: MAXNODS
type(node) :: NODES(MAXNODS)
integer :: i,j,k
write(*,*) 'MAXNODS = ',MAXNODS
do i=1,MAXNODS
NODES(i)%order = i
do j = 1,5
NODES(i)%zdofs(j) = i+j
enddo
enddo
return
end
and Here is the example C++ code
#include <iostream>
using namespace std;
class node_struct {
public:
int order;
double *zdofs;
};
typedef node_struct *node;
extern "C" {
void test_fortran_sub_(node NODES,int *MAXNODS);
}
int main(void)
{
int MAXNODS = 1;
node NODES;
NODES = new node_struct[MAXNODS];
for (int i = 0; i < MAXNODS; i++ ) {
NODES.zdofs = new double[5];
}
test_fortran_sub_(NODES,&MAXNODS);
for (int i = 0; i < MAXNODS; i++) {
cout << "NODES[" << i << "].order = " << NODES.order << endl;
for (int j = 0; j < 5; j++)
cout << "NODES[" << i << "].zdofs["<<j<<"] = " << NODES.zdofs[j] <<
endl;
}
return 0;
}
I am writing some progam that involves both C++ and Fortran. Some of the
existing code is in Fortran. The main program will be in C++ and it will
call some Fortran subroutine. All the memory allocation has to be done in
C++.
For arrays of structures and arrays (including multi-dimensional array) of
primitive data types declared in Fortran, I know the corresponding data
structures in C++. So I can declare those data structures in C++, allocate
them, and pass them as argument to Fortran subroutine. But when a member of
the structure is a pointer (declared in Fortran), I cannot figure out what
should be the corresponding data structures in C++. I have tried pointer to
the same type in C++, but it do not work and the Fortran subroutine will get
a segmentation fault when accessing the pointer.
I cannot change the data structures defined in Fortran. What I can do is to
use appropiate data structures in C++ and pass it to the Fortran subroutine
as argument.
Can anyone tell me what should be the data structure defined in C++ that
corresponds to the Fortran structure "node". Thanks in advance.
Here is my example fortran code
subroutine test_fortran_sub(NODES,MAXNODS)
c
implicit none
type node
integer :: order
double precision,dimension),pointer :: zdofs
end type node
integer :: MAXNODS
type(node) :: NODES(MAXNODS)
integer :: i,j,k
write(*,*) 'MAXNODS = ',MAXNODS
do i=1,MAXNODS
NODES(i)%order = i
do j = 1,5
NODES(i)%zdofs(j) = i+j
enddo
enddo
return
end
and Here is the example C++ code
#include <iostream>
using namespace std;
class node_struct {
public:
int order;
double *zdofs;
};
typedef node_struct *node;
extern "C" {
void test_fortran_sub_(node NODES,int *MAXNODS);
}
int main(void)
{
int MAXNODS = 1;
node NODES;
NODES = new node_struct[MAXNODS];
for (int i = 0; i < MAXNODS; i++ ) {
NODES.zdofs = new double[5];
}
test_fortran_sub_(NODES,&MAXNODS);
for (int i = 0; i < MAXNODS; i++) {
cout << "NODES[" << i << "].order = " << NODES.order << endl;
for (int j = 0; j < 5; j++)
cout << "NODES[" << i << "].zdofs["<<j<<"] = " << NODES.zdofs[j] <<
endl;
}
return 0;
}