- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello!
I havethe following source code, which deals with simple addition.
subroutine add(a,b,c)
implicit none
real, intent(in) :: a, b
real, intent(out) :: c
integer :: i, j
c = a + b
end subroutine add
subroutine add_array(dim1,dim2,a,b,c)
implicit none
integer, intent(in) :: dim1, dim2
real, dimension(dim1,dim2), intent(in) :: a, b
real, dimension(dim1,dim2), intent(out) :: c
integer :: i, j
c = a + b
end subroutine add_array
program main
implicit none
real :: a, b, c
real, dimension(2,2) :: x, y, z
a = 1.0
b = 2.0
call add(a, b, c)
print*,"c=",c
x = 1.0
y = 2.0
call add_array(2, 2, x, y, z)
print*,"z(1,1)=",z(1,1)
end program main
If I set the compiler option to STDCALL, build and runthe program, then the scalar result "c" is not updated, but the array "z" is updated.
Is intent(out) not sufficient to make the compiler know that I want to pass "c" by reference?
Arrays are always passed by reference because a pointer to the start of the array is passed into the procedure, am I right?
Is the default calling convention the same as the CVF convention?
Daniel.
I havethe following source code, which deals with simple addition.
subroutine add(a,b,c)
implicit none
real, intent(in) :: a, b
real, intent(out) :: c
integer :: i, j
c = a + b
end subroutine add
subroutine add_array(dim1,dim2,a,b,c)
implicit none
integer, intent(in) :: dim1, dim2
real, dimension(dim1,dim2), intent(in) :: a, b
real, dimension(dim1,dim2), intent(out) :: c
integer :: i, j
c = a + b
end subroutine add_array
program main
implicit none
real :: a, b, c
real, dimension(2,2) :: x, y, z
a = 1.0
b = 2.0
call add(a, b, c)
print*,"c=",c
x = 1.0
y = 2.0
call add_array(2, 2, x, y, z)
print*,"z(1,1)=",z(1,1)
end program main
If I set the compiler option to STDCALL, build and runthe program, then the scalar result "c" is not updated, but the array "z" is updated.
Is intent(out) not sufficient to make the compiler know that I want to pass "c" by reference?
Arrays are always passed by reference because a pointer to the start of the array is passed into the procedure, am I right?
Is the default calling convention the same as the CVF convention?
Daniel.
Link Copied
9 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
For consistency with CVF, ifort 32-bit only has the option /iface:cvf. This is partly consistent with stdcall, an interface which Microsoft obsoleted about 9 years ago.
If you're engaging in a learning process, your time would be better spent on standard methods, including iso_c_binding.
If you're engaging in a learning process, your time would be better spent on standard methods, including iso_c_binding.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
STDCALL sets the default to pass-by-value. No, INTENT(OUT) does not change this. Yes, arrays are passed by reference (or by descriptor).
The default convention is not the same as CVF. The default is C, REFERENCE with string lengths at the end of the argument list.
Do you have a particular need for STDCALL here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - tim18
For consistency with CVF, ifort 32-bit only has the option /iface:cvf. This is partly consistent with stdcall, an interface which Microsoft obsoleted about 9 years ago.
If you're engaging in a learning process, your time would be better spent on standard methods, including iso_c_binding.
If you're engaging in a learning process, your time would be better spent on standard methods, including iso_c_binding.
Thank you, Tim18. What are the standard methods?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
STDCALL sets the default to pass-by-value. No, INTENT(OUT) does not change this. Yes, arrays are passed by reference (or by descriptor).
The default convention is not the same as CVF. The default is C, REFERENCE with string lengths at the end of the argument list.
Do you have a particular need for STDCALL here?
Thank you Steve. I'm writting a FORTRAN DLL to be called from C# project. I heard that STDCALL is one of the calling conventions used for DLLs. Are there any new, more modern calling conventions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's not a case of "new", it's just that Microsoft defined two different conventions on 32-bit Windows and you have to know which one to use when. Usually when calling DLLs from .NET languages one needs STDCALL, but I am not sure about C#, as the examples I have seen don't use STDCALL, but sometimes you can't tell there's a problem until after several calls. If you call a "C" routine from a caller that expects STDCALL, you'll grow the stack unnecessarily and possibly lead to errors after the call returns. The reverse will corrupt your stack.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
It's not a case of "new", it's just that Microsoft defined two different conventions on 32-bit Windows and you have to know which one to use when. Usually when calling DLLs from .NET languages one needs STDCALL, but I am not sure about C#, as the examples I have seen don't use STDCALL, but sometimes you can't tell there's a problem until after several calls. If you call a "C" routine from a caller that expects STDCALL, you'll grow the stack unnecessarily and possibly lead to errors after the call returns. The reverse will corrupt your stack.
Ok, so it is a matter of who cleans after the call. Is the C, REFERENCE the same as the Microsoft cdecl?
Ok, I think I've just answered that: _cdecl passes variables by value by default.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
C always passes variables by value by default.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve, Tim18,
Thank you for your help yougave me during this year.
I wish you a Merry Christmas and a Happy New Year!
Steve please pass on these wishes to Lorri Menard.
All the best,
Daniel.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Daniel, you are very welcome. All the best to you too.

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