- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
Can't run a mixed language program with dynamical array.
CPP call FORTRAN as listed below.
CPP main program calls a fortran sub with the dynamic 2d array passing.
Compiling and linking phase was successful. However,
the following message is displayed, after execution
"segmentation fault: 11"
WHAT'S HAPPENED?
Thank you very much for help.
Malik.
#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;
const int MAXSIZE = 10;
extern "C"
{
void fsub(int**, int, int);
}
int main()
{
int *A[MAXSIZE], size;
int i, j, ir, jc;
cout<<"\nRow size:";
cin>>ir;
cout<<"\nCol size:";
cin>>jc;
for( i=0;i<ir;++i)
{
A = new int[jc];
assert(A != 0);
}
for(i=0; i<ir; ++i)
for(j=0; j<jc; ++j)
{
A
cout<<"\nA["<<i<<"]"<<"["<<j<<"]= "<<A
}
cout<<endl<<"FORTRAN calling"<<endl;
fsub(A,ir,jc);
for(i=0; i<ir; i++)
{
for(j=0; j<jc; j++)
cout<<"A["<<i<<"]["<<j<<"]="<<A
}
delete[] *A;
return EXIT_SUCCESS;
}
FORTRAN SIDE,
SUBROUTINE FSUB(A, ROWS, COLS)BIND(C,name="fsub")
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
INTEGER(KIND=C_INT), INTENT(IN), VALUE :: ROWS, COLS
INTEGER(KIND=C_INT), INTENT(INOUT) :: A(10,COLS)
INTEGER :: I, J
A=TRANSPOSE(A)
DO I =1, ROWS
DO J = 1, COLS
A(I,J) = A(I,J)*100;
ENDDO
ENDDO
END
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
On the C/C++ side, allocate the total amount of data in one allocation:
int* blob = new int[ir*jc]; assert(blob!=0); for(int i=0;i<ir;++i) A = &blob[i*jc]; ...
The A array of int* is available to C++, the call to Fortran would pass the base (blob) and the two dimensions ir and jc. Fortran would then construct the appropriate array descriptor from the dummy arguments.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you both, Tim and Jim!
I'm understand that interchanging arrays with the CPP and FORTRAN can be allowed via a 'flat' arrays only. :(
Malik.
PS. I has programmed in Symantec C++(Zortec C++) many years ago. Symantec C++ was supported by NCEG (Numerical C Extension Group)
There was an extern "FORTRAN" name conversions, instead the extern "C" above.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page