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

StackOverflow Exception when Called from Managed Code

Ngu_Soon_Hui
Beginner
850 Views
I asked this question here. But since this is more relevant to fortran compiler, so I reask the question again.

Basically, I have fortran dll as my backend, and C# as my front end. My backend is something like this ( trimmed down):
[fxfortran] subroutine chartest(maxncv,ldv)

  !DEC$ ATTRIBUTES DLLEXPORT::chartest
  !DEC$ ATTRIBUTES ALIAS:'chartest'::chartest
   !DEC$ ATTRIBUTES VALUE :: maxncv,ldv

 &    

   integer, intent(in)  :: maxncv, ldv

  Double precision
 &                  v(ldv,maxncv),d(maxncv,2)

   print *, 'hello'

  end[/fxfortran]
And my C# code is something like this:
[bash]  public static extern void chartest(

       [MarshalAs(UnmanagedType.I4)] int maxncv,
          [MarshalAs(UnmanagedType.I4)] int ldv


   );[/bash]
If I callchartest(546, 547), I would get a stackoverflowexception.
I understand and verify that by turning on the/heap_arrays I can overcome this exception, but from what I've read, my code shouldn't generate any stackoverflowexception, since the array assignment is done inside fortran code. Is there anything missing here?
If not then I have to turn on the \\heap_arrays option as mentioned. Is there is a better solution?
I'm using version 11.0.066
0 Kudos
5 Replies
DavidWhite
Valued Contributor II
850 Views
Suggest you search the forum for C#. From what I can see, you need to use REFERENCE rather than VALUE:

By default scalar type like integer is passed by reference in fortran so its corresponding in C# should be declared as reference type.

Regards,

David
0 Kudos
Ngu_Soon_Hui
Beginner
850 Views
Thanks for your advice, but I'm not sure whether by changing to "passed by reference ", will it solve my problem?
For small input parameters, my code can run fine, so I don't think pass by reference/ value is the problem here.
0 Kudos
DavidWhite
Valued Contributor II
850 Views
My concern was about the value of the array size which you are passing as an argument. Is the value on in C# and in Fortran the same or is it being corrupted? You may need to use STDCALL for the argument passing as well.

Regards,


David
0 Kudos
Ngu_Soon_Hui
Beginner
850 Views
David,


But it were corrupted, why for small values of ints, there were no sign of corruption?
Also, in my fortran code, I explicitly specify the pass in parameters as value:
[fortran]!DEC$ ATTRIBUTES VALUE :: maxncv,ldv  [/fortran]
So I don't think there is any issue of corruption
0 Kudos
Andrew_Smith
Valued Contributor I
850 Views
Your Fortran local arrays d and v are "automatic" which means they will be created on the stack by default. You can either use /heap_arrays or change the arrays to allocatable and add code to manage the allocate and deallocate.
0 Kudos
Reply