You can pass data between Fortran and C, C++, and MASM through calling argument lists just as you can within each language (for example, the argument list a, b and c in CALL MYSUB(a,b,c)). There are two ways to pass individual arguments:
By value, which passes the argument's value.
By reference, which passes the address of the arguments. On systems based on IA-32 architecture, Fortran, C, and C++ use 4-byte addresses. On systems based on Intel® 64 architecture, these languages use 8-byte addresses.
You need to make sure that for every call, the calling program and the called routine agree on how each argument is passed. Otherwise, the called routine receives bad data.
The Fortran technique for passing arguments changes depending on the calling convention specified. By default, Fortran passes all data by reference (except the hidden length argument of strings, which is passed by value).
If the ATTRIBUTES C option or, for Windows OS, the STDCALL option is used, the default changes to passing all data by value except arrays. If the procedure has the REFERENCE option as well as the C or STDCALL option, all arguments by default are passed by reference.
You can specify also argument options, VALUE and REFERENCE, to pass arguments by value or by reference. In mixed-language programming, it is a good idea to specify the passing technique explicitly rather than relying on defaults.
On Windows operating systems, the compiler option /iface also establishes some default argument passing conventions (such as for hidden length of strings). See ATTRIBUTES Properties and Calling Conventions.
Examples of passing by reference and value for C and MASM follow. All are interfaces to the example Fortran subroutine TESTPROC below. The definition of TESTPROC declares how each argument is passed. The REFERENCE option is not strictly necessary in this example, but using it makes the argument's passing convention conspicuous.
SUBROUTINE TESTPROC( VALPARM, REFPARM ) !DEC$ ATTRIBUTES VALUE :: VALPARM !DEC$ ATTRIBUTES REFERENCE :: REFPARM INTEGER VALPARM INTEGER REFPARM END SUBROUTINE
In C and C++, all arguments are passed by value, except arrays, which are passed by reference to the address of the first member of the array. Unlike Fortran, C and C++ do not have calling-convention directives to affect the way individual arguments are passed. To pass non-array C data by reference, you must pass a pointer to it. To pass a C array by value, you must declare it as a member of a structure and pass the structure. The following C declaration sets up a call to the example Fortran TESTPROC subroutine:
For Linux OS:
extern void testproc_( int ValParm, int *RefParm );
For Windows OS:
extern void TESTPROC( int ValParm, int *RefParm );
In MASM (Windows OS only), arguments are passed by value by default. Arguments to be passed by reference are designated with PTR in the PROTO and PROC directives. For example:
TESTPROC PROTO, valparm: SDWORD, refparm: PTR SDWORD
To use an argument passed by value, use the value of the variable. For example:
mov eax, valparm ; Load value of argument
This statement places the value of valparm into the EAX register.
To use an argument passed by reference, use the address of the variable. For example:
mov ecx, refparm ; Load address of argument mov eax, [ecx] ; Load value of argument
These statements place the value of refparm into the EAX register.
The following table summarizes how to pass arguments by reference and value. An array name in C is equated to its starting address because arrays are normally passed by reference. You can assign the REFERENCE property to a procedure, as well as to individual arguments.
Language |
ATTRIBUTE |
Argument Type |
To Pass by Reference |
To Pass by Value |
---|---|---|---|---|
Fortran |
Default |
Scalars and derived types |
Default |
VALUE option |
C (or, for Windows OS, STDCALL) option |
Scalars and derived types |
REFERENCE option |
Default |
|
Default |
Arrays |
Default |
Cannot pass by value |
|
C (or, for Windows OS, STDCALL) option |
Arrays |
Default |
Cannot pass by value |
|
C/C++ |
Non-arrays |
Pointer argument_name |
Default |
|
Arrays |
Default |
Struct {type} array_name |
||
Assembler MASM (Windows OS only) |
All types |
PTR |
Default |
This table does not describe argument passing of strings and Fortran 95/90 pointer arguments in Intel Fortran, which are constructed differently than other arguments. By default, Fortran passes strings by reference along with the string length. String length placement depends on whether the compiler option -mixed-str-len-arg (Linux OS and Mac OS X) or /iface:mixed_str_len_arg (Windows OS) is set immediately after the address of the beginning of the string. It also depends on whether -nomixed-str-len-arg (Linux OS and Mac OS X) or /iface:nomixed_str_len_arg (Windows OS) is set after all arguments.
Fortran 95/90 array pointers and assumed-shape arrays are passed by passing the address of the array descriptor.
For a discussion of the effect of attributes on passing Fortran 95/90 pointers and strings, see Handling Fortran Array Pointers and Allocatable Arrays and Handling Character Strings.
Copyright © 1996-2010, Intel Corporation. All rights reserved.