Working-with-Pointers.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1999-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>The GNU Fortran Compiler: Working with Pointers</title>
  20. <meta name="description" content="The GNU Fortran Compiler: Working with Pointers">
  21. <meta name="keywords" content="The GNU Fortran Compiler: Working with Pointers">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Interoperability-with-C.html#Interoperability-with-C" rel="up" title="Interoperability with C">
  30. <link href="Further-Interoperability-of-Fortran-with-C.html#Further-Interoperability-of-Fortran-with-C" rel="next" title="Further Interoperability of Fortran with C">
  31. <link href="Interoperable-Subroutines-and-Functions.html#Interoperable-Subroutines-and-Functions" rel="prev" title="Interoperable Subroutines and Functions">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Working-with-Pointers"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Further-Interoperability-of-Fortran-with-C.html#Further-Interoperability-of-Fortran-with-C" accesskey="n" rel="next">Further Interoperability of Fortran with C</a>, Previous: <a href="Interoperable-Subroutines-and-Functions.html#Interoperable-Subroutines-and-Functions" accesskey="p" rel="prev">Interoperable Subroutines and Functions</a>, Up: <a href="Interoperability-with-C.html#Interoperability-with-C" accesskey="u" rel="up">Interoperability with C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Working-with-Pointers-1"></a>
  69. <h4 class="subsection">7.1.5 Working with Pointers</h4>
  70. <p>C pointers are represented in Fortran via the special opaque derived type
  71. <code>type(c_ptr)</code> (with private components). Thus one needs to
  72. use intrinsic conversion procedures to convert from or to C pointers.
  73. </p>
  74. <p>For some applications, using an assumed type (<code>TYPE(*)</code>) can be an
  75. alternative to a C pointer; see
  76. <a href="Further-Interoperability-of-Fortran-with-C.html#Further-Interoperability-of-Fortran-with-C">Further Interoperability of Fortran with C</a>.
  77. </p>
  78. <p>For example,
  79. </p>
  80. <div class="smallexample">
  81. <pre class="smallexample"> use iso_c_binding
  82. type(c_ptr) :: cptr1, cptr2
  83. integer, target :: array(7), scalar
  84. integer, pointer :: pa(:), ps
  85. cptr1 = c_loc(array(1)) ! The programmer needs to ensure that the
  86. ! array is contiguous if required by the C
  87. ! procedure
  88. cptr2 = c_loc(scalar)
  89. call c_f_pointer(cptr2, ps)
  90. call c_f_pointer(cptr2, pa, shape=[7])
  91. </pre></div>
  92. <p>When converting C to Fortran arrays, the one-dimensional <code>SHAPE</code> argument
  93. has to be passed.
  94. </p>
  95. <p>If a pointer is a dummy-argument of an interoperable procedure, it usually
  96. has to be declared using the <code>VALUE</code> attribute. <code>void*</code>
  97. matches <code>TYPE(C_PTR), VALUE</code>, while <code>TYPE(C_PTR)</code> alone
  98. matches <code>void**</code>.
  99. </p>
  100. <p>Procedure pointers are handled analogously to pointers; the C type is
  101. <code>TYPE(C_FUNPTR)</code> and the intrinsic conversion procedures are
  102. <code>C_F_PROCPOINTER</code> and <code>C_FUNLOC</code>.
  103. </p>
  104. <p>Let us consider two examples of actually passing a procedure pointer from
  105. C to Fortran and vice versa. Note that these examples are also very
  106. similar to passing ordinary pointers between both languages. First,
  107. consider this code in C:
  108. </p>
  109. <div class="smallexample">
  110. <pre class="smallexample">/* Procedure implemented in Fortran. */
  111. void get_values (void (*)(double));
  112. /* Call-back routine we want called from Fortran. */
  113. void
  114. print_it (double x)
  115. {
  116. printf (&quot;Number is %f.\n&quot;, x);
  117. }
  118. /* Call Fortran routine and pass call-back to it. */
  119. void
  120. foobar ()
  121. {
  122. get_values (&amp;print_it);
  123. }
  124. </pre></div>
  125. <p>A matching implementation for <code>get_values</code> in Fortran, that correctly
  126. receives the procedure pointer from C and is able to call it, is given
  127. in the following <code>MODULE</code>:
  128. </p>
  129. <div class="smallexample">
  130. <pre class="smallexample">MODULE m
  131. IMPLICIT NONE
  132. ! Define interface of call-back routine.
  133. ABSTRACT INTERFACE
  134. SUBROUTINE callback (x)
  135. USE, INTRINSIC :: ISO_C_BINDING
  136. REAL(KIND=C_DOUBLE), INTENT(IN), VALUE :: x
  137. END SUBROUTINE callback
  138. END INTERFACE
  139. CONTAINS
  140. ! Define C-bound procedure.
  141. SUBROUTINE get_values (cproc) BIND(C)
  142. USE, INTRINSIC :: ISO_C_BINDING
  143. TYPE(C_FUNPTR), INTENT(IN), VALUE :: cproc
  144. PROCEDURE(callback), POINTER :: proc
  145. ! Convert C to Fortran procedure pointer.
  146. CALL C_F_PROCPOINTER (cproc, proc)
  147. ! Call it.
  148. CALL proc (1.0_C_DOUBLE)
  149. CALL proc (-42.0_C_DOUBLE)
  150. CALL proc (18.12_C_DOUBLE)
  151. END SUBROUTINE get_values
  152. END MODULE m
  153. </pre></div>
  154. <p>Next, we want to call a C routine that expects a procedure pointer argument
  155. and pass it a Fortran procedure (which clearly must be interoperable!).
  156. Again, the C function may be:
  157. </p>
  158. <div class="smallexample">
  159. <pre class="smallexample">int
  160. call_it (int (*func)(int), int arg)
  161. {
  162. return func (arg);
  163. }
  164. </pre></div>
  165. <p>It can be used as in the following Fortran code:
  166. </p>
  167. <div class="smallexample">
  168. <pre class="smallexample">MODULE m
  169. USE, INTRINSIC :: ISO_C_BINDING
  170. IMPLICIT NONE
  171. ! Define interface of C function.
  172. INTERFACE
  173. INTEGER(KIND=C_INT) FUNCTION call_it (func, arg) BIND(C)
  174. USE, INTRINSIC :: ISO_C_BINDING
  175. TYPE(C_FUNPTR), INTENT(IN), VALUE :: func
  176. INTEGER(KIND=C_INT), INTENT(IN), VALUE :: arg
  177. END FUNCTION call_it
  178. END INTERFACE
  179. CONTAINS
  180. ! Define procedure passed to C function.
  181. ! It must be interoperable!
  182. INTEGER(KIND=C_INT) FUNCTION double_it (arg) BIND(C)
  183. INTEGER(KIND=C_INT), INTENT(IN), VALUE :: arg
  184. double_it = arg + arg
  185. END FUNCTION double_it
  186. ! Call C function.
  187. SUBROUTINE foobar ()
  188. TYPE(C_FUNPTR) :: cproc
  189. INTEGER(KIND=C_INT) :: i
  190. ! Get C procedure pointer.
  191. cproc = C_FUNLOC (double_it)
  192. ! Use it.
  193. DO i = 1_C_INT, 10_C_INT
  194. PRINT *, call_it (cproc, i)
  195. END DO
  196. END SUBROUTINE foobar
  197. END MODULE m
  198. </pre></div>
  199. <hr>
  200. <div class="header">
  201. <p>
  202. Next: <a href="Further-Interoperability-of-Fortran-with-C.html#Further-Interoperability-of-Fortran-with-C" accesskey="n" rel="next">Further Interoperability of Fortran with C</a>, Previous: <a href="Interoperable-Subroutines-and-Functions.html#Interoperable-Subroutines-and-Functions" accesskey="p" rel="prev">Interoperable Subroutines and Functions</a>, Up: <a href="Interoperability-with-C.html#Interoperability-with-C" accesskey="u" rel="up">Interoperability with C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  203. </div>
  204. </body>
  205. </html>