- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
void CMyClass::myCallToFortran()
{
int *cintArray = (int *)malloc(100);
cintArray[0]=1;
cintArray[1]=2;
cintArray[2]=3;
int arrayCount = 100;
myfcn90(cintArray,&arrayCount);
}
Then, my FORTRAN code, which crashes when I attempt to access the array is:
subroutine fcn90(cintArray,arrayCount) bind(C,name='myfcn90')
use, intrinsic :: iso_c_binding
type(c_ptr), intent(in):: cintArray
integer*4, intent(in) ::arrayCount
integer*4, dimension(:),pointer :: a
integer i,k
call c_f_pointer(cintArray,a,(/arrayCount/))
k = a(1) <= CRASH occurs right here. Debugger shows thatthe values are "undefined Addresses". Run time error is 80010105
end subroutine
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
void CMyClass::myCallToFortran()
{
int *cintArray = (int *)malloc(100);
cintArray[0]=1;
cintArray[1]=2;
cintArray[2]=3;
int arrayCount = 100;
myfcn90(cintArray,&arrayCount);
}
Then, my FORTRAN code, which crashes when I attempt to access the array is:
subroutine fcn90(cintArray,arrayCount) bind(C,name='myfcn90')
use, intrinsic :: iso_c_binding
type(c_ptr), intent(in):: cintArray
integer*4, intent(in) ::arrayCount
integer*4, dimension(:),pointer :: a
integer i,k
call c_f_pointer(cintArray,a,(/arrayCount/))
k = a(1) <= CRASH occurs right here. Debugger shows thatthe values are "undefined Addresses". Run time error is 80010105
end subroutine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
void CMyClass::myCallToFortran()
{
int *cintArray = (int *)malloc(100*sizeof(int));
cintArray[0]=1;
cintArray[1]=2;
cintArray[2]=3;
int arrayCount = 100;
myfcn90(cintArray,&arrayCount);
}
Then, my FORTRAN code, which crashes when I attempt to access the array is:
subroutine fcn90(cintArray,arrayCount) bind(C,name='myfcn90')
use, intrinsic :: iso_c_binding
type(c_ptr), intent(in):: cintArray
integer*4, intent(in) ::arrayCount
integer*4, dimension(:),pointer :: a
integer i,k
call c_f_pointer(cintArray,a,(/arrayCount/))
k = a(1) <= CRASH occurs right here. Debugger shows thatthe values are "undefined Addresses". Run time error is 80010105
end subroutine
Malloc should have read malloc(100*sizeof(int)).
I have also tried new int[100];
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You are passing the pointer out of C/C++ by value but your fortran subroutine receives it (by default) by reference. Instead:
TYPE(C_PTR), INTENT(IN), VALUE :: cintArray
An unrelated technicality is that the integers on the fortran side should have kind C_INT.
IanH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page