next up previous 252
Next: Allocating Exportable Dynamic Memory
Up: Pointers
Previous: Pointers


Pointer Registration and Conversion

To overcome these problems, some method is needed of converting between (say) 64-bit C pointers and the typical 32 bits of a FORTRAN INTEGER. The same method must also work if the two pointer representations are actually of equal length. To allow this, CNF maintains an internal table which contains all the C pointers which will be exported and used from FORTRAN. The pointers stored in this table are said to have been ``registered'' for use from both C and FORTRAN.

When converting a C pointer into a FORTRAN pointer, it is sufficient simply to mask out all bits except those that will fit into a FORTRAN INTEGER. This is performed by the function cnfFptr. When converting in the opposite direction, the internal table must be searched to locate a pointer which has the same value stored in the set of masked bits (e.g. the lowest 32 bits) as the FORTRAN pointer value. The full value of the C pointer can then be read from the table. This conversion is performed by cnfCptr.

Apart from the requirement that all pointers which will be used from both C and FORTRAN must be registered by entering them in the internal table, this scheme also requires that all registered pointers should be unique in their lowest 32 bits (or whatever length a FORTRAN INTEGER has) in order for the conversion from FORTRAN to C to select a unique pointer from the table. In practice, these requirements are most easily fulfilled by providing a set of memory allocation functions in CNF which mirror the standard C run time library functions malloc, calloc and free.



next up previous 252
Next: Allocating Exportable Dynamic Memory
Up: Pointers
Previous: Pointers

CNF and F77 Mixed Language Programming -- FORTRAN and C
Starlink User Note 209
P.M. Allan
A.J. Chipperfield
R.F. Warren-Smith
19 January 2000
E-mail:ussc@star.rl.ac.uk