- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi All,
I have a Vs2008 project where some arrays are allocted on the heap.
These will be used in a Fortran DLL.
Use of Integer and Real arrays works fine, but when I try to use the character array ,the app is crashing
in the fortran DLL
This apprach works fine when compiling as a 32-bit app.
The Cpp code:
extern "C" int _declspec(dllimport) __stdcall S34073(int *n,float *a,int *na,char *ca,int len_ca,int *iret);
int _tmain(int argc, _TCHAR* argv[])
{
static int len_ca=4;
int n;
n=1000000;
static int * dummy=NULL;
dummy=new int;
int iret;
float * a= (float *) dummy;
int * na= (int *) dummy;
char * ca= (char *) dummy;
strcpy(ca,"ZERO");
strcpy((ca+4),"ONE1");
strcpy((ca+8),"TWO2");
strcpy((ca+12),"THRE");
S34073(&n,a,na,ca,len_ca,&iret);
return 0;
}
and the fortran code
SUBROUTINE S34073 (NS,A,NA,CA,IRET)
!DEC$ ATTRIBUTES DLLEXPORT::S34073
DIMENSION A(1),NA(1),CA(1)
CHARACTER*(*) CA
lenc=LEN(CA) // The lenc is correct=4
NA(1)=1111
A(2)=2.2222
CA(3)='HELP' // the app crashes here when I try to do anything to the CA vector.
RETURN
END
I enclosure also a small testproject
I hope someone has some idea...
/Lars
I have a Vs2008 project where some arrays are allocted on the heap.
These will be used in a Fortran DLL.
Use of Integer and Real arrays works fine, but when I try to use the character array ,the app is crashing
in the fortran DLL
This apprach works fine when compiling as a 32-bit app.
The Cpp code:
extern "C" int _declspec(dllimport) __stdcall S34073(int *n,float *a,int *na,char *ca,int len_ca,int *iret);
int _tmain(int argc, _TCHAR* argv[])
{
static int len_ca=4;
int n;
n=1000000;
static int * dummy=NULL;
dummy=new int
int iret;
float * a= (float *) dummy;
int * na= (int *) dummy;
char * ca= (char *) dummy;
strcpy(ca,"ZERO");
strcpy((ca+4),"ONE1");
strcpy((ca+8),"TWO2");
strcpy((ca+12),"THRE");
S34073(&n,a,na,ca,len_ca,&iret);
return 0;
}
and the fortran code
SUBROUTINE S34073 (NS,A,NA,CA,IRET)
!DEC$ ATTRIBUTES DLLEXPORT::S34073
DIMENSION A(1),NA(1),CA(1)
CHARACTER*(*) CA
lenc=LEN(CA) // The lenc is correct=4
NA(1)=1111
A(2)=2.2222
CA(3)='HELP' // the app crashes here when I try to do anything to the CA vector.
RETURN
END
I enclosure also a small testproject
I hope someone has some idea...
/Lars
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You passed len_ca immediately following ca - that's not how Intel Fortran does it by default. Put the length argument at the end of the argument list. I will also suggest that you declare it as intptr_t rather than int, otherwise this will fail on a 64-bit platform.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Steve,
I use the compiler option CVF (/iface:cvf) and I think the length is then passed immediately after the argument.
And if I run the debugger I can verify that lenca is =4 as expected.
but I will change lenght argument to intptr_t and test again.
/Lars
I use the compiler option CVF (/iface:cvf) and I think the length is then passed immediately after the argument.
And if I run the debugger I can verify that lenca is =4 as expected.
but I will change lenght argument to intptr_t and test again.
/Lars
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
int is ok if you are doing 32-bit.
I tried your code as-is and it worked for me. How does it "crash"?
I would suggest, as a matter of style, to dimension the arrays in Fortran as (*) rather than (1) - the latter is an old F66 trick that is supported but obsoleted 22 years ago by assumed-size arrays in FORTRAN-77.
I tried your code as-is and it worked for me. How does it "crash"?
I would suggest, as a matter of style, to dimension the arrays in Fortran as (*) rather than (1) - the latter is an old F66 trick that is supported but obsoleted 22 years ago by assumed-size arrays in FORTRAN-77.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Steve,
I changed the length of ca from int to intptr_t and now its working as I expects.
/Lars
I changed the length of ca from int to intptr_t and now its working as I expects.
/Lars
![](/skins/images/045A6C88D0527A93E76B179D7F5E2AFE/responsive_peak/images/icon_anonymous_message.png)
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page