We have already seen that character strings and LOGICAL
and POINTER
variables have to be converted
between the different forms used by FORTRAN and C, and the idea of “importing” a FORTRAN
value to a C value, and “exporting” a C value to a FORTRAN value has been introduced with the CNF
routines..
Potentially all the other types could differ so macros F77_IMPORT_type
, F77_IMPORT_type_ARRAY
,
F77_EXPORT_type
and F77_EXPORT_type_ARRAY
are defined to copy the data as required – they will
use CNF routines where appropriate. An additional type
of LOCATOR
is allowed for the
IMPORT/EXPORT
macros to handle character strings used as HDS locators. There are also macros
F77_IMPORT_CHARACTER_ARRAY_P
and F77_EXPORT_CHARACTER_ARRAY_P
to handle the CHARACTER
conversion if the C array is an array of pointers to char.
The IMPORT/EXPORT_ARRAY
macros have arguments giving pointers to the data and the number of
elements to be converted. This is assumed to be sufficient for both single and multi-dimensional
arrays.
These macros impose a slight overhead in that they require both the FORTRAN and C variables to be set up and some copying done, even when this is not strictly necessary. However, they do protect against possible future problems and ease the problem of deciding whether and how the import/export should be done.
In the case of arrays, only pointers are copied unless a conversion really is required (as in the case of
CHARACTER
and LOGICAL
arrays, for example).
A complication arises where the actual argument for a FORTRAN subroutine to be called from C is an
array which is only returned. In that case, no exporting is required but the FORTRAN
array must still be associated with the C array so that the FORTRAN subroutine knows
where to store the results. For those types which require genuine conversion, a pointer
to the FORTRAN array will have been set when the space was allocated but for others
the pointer must be set to point to the actual C array. Macros F77_ASSOC_type_ARRAY
are
defined to do this where necessary. They are complementary to the F77_CREATE_type_ARRAY
macros so you can include both to ensure that the pointer to the FORTRAN array is set
correctly.
After use, the memory holding the FORTRAN array should be returned using an F77_FREE_type
macro (which will do nothing if the CREATE
macros for the type do not allocate space).
So, a C wrapper for the FORTRAN routine str_reset
in the section on Handling CHARACTER
and
LOGICAL
arrays could be written as follows:
IMPORT/EXPORT
macros.