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

STDCALL and INTENT(OUT)

Ilie__Daniel
Beginner
1,030 Views
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.
0 Kudos
9 Replies
TimP
Honored Contributor III
1,030 Views
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.
0 Kudos
Steven_L_Intel1
Employee
1,030 Views

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?
0 Kudos
Ilie__Daniel
Beginner
1,030 Views
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.

Thank you, Tim18. What are the standard methods?
0 Kudos
Ilie__Daniel
Beginner
1,030 Views

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?

0 Kudos
Steven_L_Intel1
Employee
1,030 Views

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.
0 Kudos
Ilie__Daniel
Beginner
1,030 Views

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.
0 Kudos
Steven_L_Intel1
Employee
1,030 Views

C always passes variables by value by default.
0 Kudos
Ilie__Daniel
Beginner
1,030 Views

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.
0 Kudos
Steven_L_Intel1
Employee
1,030 Views

Daniel, you are very welcome. All the best to you too.
0 Kudos
Reply