- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 call
chartest(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
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Regards,
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

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