Call dynamic array from fortrant 77 in C++ ?

I have some problems with combination between Fortran and C++> Hence I can not link the data between two codes. Please take a look on my source code and help me to clarify the problem.

This is my source
--- Code Fortrant: source_f.f -----
subroutine loadarr2(nx,ny,arr)
integer nx,ny,i,j,temp
real arr(nx,ny)
temp=0
write(*,*)'Print to Screen Array 2D in Fortran'
write(*,*)'nx= ',nx,'\tny= ',ny
do i=1,nx
do j=1,ny
write(*,*)arr(i,j)
enddo
enddo
end

subroutine rearr2(nx,ny,arr)
integer nx,ny,i,j,temp
real arr(nx,ny)
nx=3
ny=3
temp=0
do i=1,nx
do j=1,ny
temp=temp+1
arr(i,j)=temp
enddo
enddo
end

subroutine loadarr3(nx,ny,nz,arr)
integer nx,ny,nz,i,j,k,temp
real arr(nx,ny,nz)
temp=0
write(*,*)'Print to Screen Array 3D in Fortran'
write(*,*)'nx= ',nx,'\tny= ',ny,'\tnz= ',nz
do i=1,nx
do j=1,ny
do k=1,nz
write(*,*)arr(i,j,k)
enddo
enddo
enddo
end

subroutine rearr3(nx,ny,nz,arr)
integer nx,ny,nz,i,j,k,temp
real arr(nx,ny,nz)
temp=0
nx=4
ny=4
nz=4
do i=1,nx
do j=1,ny
do k=1,nz
temp=temp+1
arr(i,j,k)=temp
enddo
enddo
enddo
end


--- Code C++: source_c.C -----
#include <iostream>
#include <stdlib.h>
using namespace std;
extern "C"{
void rearr2_(int *,int *,float **);
void loadarr2_(int &,int &,float **);
void rearr3_(int *,int *,int *,float ***);
void loadarr3_(int &,int &,int &,float ***);
}
int main(){
int nx,ny,nz,i,j,k,temp;
float **arr2d_load,**arr2d_retrieve;
float ***arr3d_load,***arr3d_retrieve;
nx=ny=nz=4;
temp=0;
//----------------------- Array 2d
//------------------- Allocate array 2d
arr2d_load=(float**)malloc(nx*sizeof(float*));
for(i=0;i<nx;i++)
arr2d_load[i]=(float*)malloc(ny*sizeof(float));

arr2d_retrieve=(float**)malloc(nx*sizeof(float*));
for(i=0;i<nx;i++)
arr2d_retrieve[i]=(float*)malloc(ny*sizeof(float));

//-------------------
nx=ny=2;
for(i=0;i<nx;i++)
for(j=0;j<ny;j++){
temp++;
arr2d_load[i][j]=temp;
}

loadarr2_(nx,ny,arr2d_load);

//-------------------
nx=ny=0;
rearr2_(&nx,&ny,arr2d_retrieve);
cerr<<"Print to Screen Array 2D in C++"<<endl;
cerr<<"nx= "<<nx<<"\tny= "<<ny<<endl;
for(i=0;i<nx;i++){
for(j=0;j<ny;j++)
cerr<<arr2d_retrieve[i][j]<<"\t";
cerr<<endl;
}

//----------------------- Array 3d
//------------------- Allocate array 3d
nx=ny=nz=4;
arr3d_load=(float***)malloc(nx*sizeof(float**));
for(i=0;i<nx;i++){
arr3d_load[i]=(float**)malloc(ny*sizeof(float*));
for(j=0;j<ny;j++)
arr3d_load[i][j]=(float*)malloc(nz*sizeof(float));
}

arr3d_retrieve=(float***)malloc(nx*sizeof(float**));
for(i=0;i<nx;i++){
arr3d_retrieve[i]=(float**)malloc(ny*sizeof(float*));
for(j=0;j<ny;j++)
arr3d_retrieve[i][j]=(float*)malloc(nz*sizeof(float));
}

//-------------------
nx=ny=nz=2;
temp=0;
for(i=0;i<nx;i++)
for(j=0;j<ny;j++)
for(k=0;k<nz;k++){
temp++;
arr3d_load[i][j][k]=temp;
}
loadarr3_(nx,ny,nz,arr3d_load);

//-------------------
nx,ny,nz=0;
rearr3_(&nx,&ny,&nz,arr3d_retrieve);
cerr<<"Print to Screen Array 2D in C++"<<endl;
cerr<<"nx= "<<nx<<"\tny= "<<ny<<"\tnz="<<nz<<endl;
for(i=0;i<nx;i++)
for(j=0;j<ny;j++){
for(k=0;k<nz;k++)
cerr<<arr3d_retrieve[i][j][k]<<"\t";
cerr<<endl;
}
//-------------------

return 1;
}

--------- Compile -----
g77 -c source_f.f
g++34 -o test source_c.C source.o -lg2c

Thanks a lot,

Topic archived. No new replies allowed.