Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28603 Discussions

Character string problem with Fortran (11.1.060) x64 dll and Vs2008 project ,Windows 7

lars1
Beginner
423 Views
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
0 Kudos
4 Replies
Steven_L_Intel1
Employee
423 Views
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.
0 Kudos
lars1
Beginner
423 Views
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
0 Kudos
Steven_L_Intel1
Employee
423 Views
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.
0 Kudos
lars1
Beginner
423 Views
Thanks Steve,
I changed the length of ca from int to intptr_t and now its working as I expects.

/Lars

0 Kudos
Reply