Cray-pointers.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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: Cray pointers</title>
  20. <meta name="description" content="The GNU Fortran Compiler: Cray pointers">
  21. <meta name="keywords" content="The GNU Fortran Compiler: Cray 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="Extensions-implemented-in-GNU-Fortran.html#Extensions-implemented-in-GNU-Fortran" rel="up" title="Extensions implemented in GNU Fortran">
  30. <link href="CONVERT-specifier.html#CONVERT-specifier" rel="next" title="CONVERT specifier">
  31. <link href="Hollerith-constants-support.html#Hollerith-constants-support" rel="prev" title="Hollerith constants support">
  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="Cray-pointers"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="CONVERT-specifier.html#CONVERT-specifier" accesskey="n" rel="next">CONVERT specifier</a>, Previous: <a href="Hollerith-constants-support.html#Hollerith-constants-support" accesskey="p" rel="prev">Hollerith constants support</a>, Up: <a href="Extensions-implemented-in-GNU-Fortran.html#Extensions-implemented-in-GNU-Fortran" accesskey="u" rel="up">Extensions implemented in GNU Fortran</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="Cray-pointers-1"></a>
  69. <h4 class="subsection">6.1.14 Cray pointers</h4>
  70. <a name="index-pointer_002c-Cray"></a>
  71. <p>Cray pointers are part of a non-standard extension that provides a
  72. C-like pointer in Fortran. This is accomplished through a pair of
  73. variables: an integer &quot;pointer&quot; that holds a memory address, and a
  74. &quot;pointee&quot; that is used to dereference the pointer.
  75. </p>
  76. <p>Pointer/pointee pairs are declared in statements of the form:
  77. </p><div class="smallexample">
  78. <pre class="smallexample"> pointer ( &lt;pointer&gt; , &lt;pointee&gt; )
  79. </pre></div>
  80. <p>or,
  81. </p><div class="smallexample">
  82. <pre class="smallexample"> pointer ( &lt;pointer1&gt; , &lt;pointee1&gt; ), ( &lt;pointer2&gt; , &lt;pointee2&gt; ), ...
  83. </pre></div>
  84. <p>The pointer is an integer that is intended to hold a memory address.
  85. The pointee may be an array or scalar. A pointee can be an assumed
  86. size array&mdash;that is, the last dimension may be left unspecified by
  87. using a <code>*</code> in place of a value&mdash;but a pointee cannot be an
  88. assumed shape array. No space is allocated for the pointee.
  89. </p>
  90. <p>The pointee may have its type declared before or after the pointer
  91. statement, and its array specification (if any) may be declared
  92. before, during, or after the pointer statement. The pointer may be
  93. declared as an integer prior to the pointer statement. However, some
  94. machines have default integer sizes that are different than the size
  95. of a pointer, and so the following code is not portable:
  96. </p><div class="smallexample">
  97. <pre class="smallexample"> integer ipt
  98. pointer (ipt, iarr)
  99. </pre></div>
  100. <p>If a pointer is declared with a kind that is too small, the compiler
  101. will issue a warning; the resulting binary will probably not work
  102. correctly, because the memory addresses stored in the pointers may be
  103. truncated. It is safer to omit the first line of the above example;
  104. if explicit declaration of ipt&rsquo;s type is omitted, then the compiler
  105. will ensure that ipt is an integer variable large enough to hold a
  106. pointer.
  107. </p>
  108. <p>Pointer arithmetic is valid with Cray pointers, but it is not the same
  109. as C pointer arithmetic. Cray pointers are just ordinary integers, so
  110. the user is responsible for determining how many bytes to add to a
  111. pointer in order to increment it. Consider the following example:
  112. </p><div class="smallexample">
  113. <pre class="smallexample"> real target(10)
  114. real pointee(10)
  115. pointer (ipt, pointee)
  116. ipt = loc (target)
  117. ipt = ipt + 1
  118. </pre></div>
  119. <p>The last statement does not set <code>ipt</code> to the address of
  120. <code>target(1)</code>, as it would in C pointer arithmetic. Adding <code>1</code>
  121. to <code>ipt</code> just adds one byte to the address stored in <code>ipt</code>.
  122. </p>
  123. <p>Any expression involving the pointee will be translated to use the
  124. value stored in the pointer as the base address.
  125. </p>
  126. <p>To get the address of elements, this extension provides an intrinsic
  127. function <code>LOC()</code>. The <code>LOC()</code> function is equivalent to the
  128. <code>&amp;</code> operator in C, except the address is cast to an integer type:
  129. </p><div class="smallexample">
  130. <pre class="smallexample"> real ar(10)
  131. pointer(ipt, arpte(10))
  132. real arpte
  133. ipt = loc(ar) ! Makes arpte is an alias for ar
  134. arpte(1) = 1.0 ! Sets ar(1) to 1.0
  135. </pre></div>
  136. <p>The pointer can also be set by a call to the <code>MALLOC</code> intrinsic
  137. (see <a href="MALLOC.html#MALLOC">MALLOC</a>).
  138. </p>
  139. <p>Cray pointees often are used to alias an existing variable. For
  140. example:
  141. </p><div class="smallexample">
  142. <pre class="smallexample"> integer target(10)
  143. integer iarr(10)
  144. pointer (ipt, iarr)
  145. ipt = loc(target)
  146. </pre></div>
  147. <p>As long as <code>ipt</code> remains unchanged, <code>iarr</code> is now an alias for
  148. <code>target</code>. The optimizer, however, will not detect this aliasing, so
  149. it is unsafe to use <code>iarr</code> and <code>target</code> simultaneously. Using
  150. a pointee in any way that violates the Fortran aliasing rules or
  151. assumptions is illegal. It is the user&rsquo;s responsibility to avoid doing
  152. this; the compiler works under the assumption that no such aliasing
  153. occurs.
  154. </p>
  155. <p>Cray pointers will work correctly when there is no aliasing (i.e., when
  156. they are used to access a dynamically allocated block of memory), and
  157. also in any routine where a pointee is used, but any variable with which
  158. it shares storage is not used. Code that violates these rules may not
  159. run as the user intends. This is not a bug in the optimizer; any code
  160. that violates the aliasing rules is illegal. (Note that this is not
  161. unique to GNU Fortran; any Fortran compiler that supports Cray pointers
  162. will &ldquo;incorrectly&rdquo; optimize code with illegal aliasing.)
  163. </p>
  164. <p>There are a number of restrictions on the attributes that can be applied
  165. to Cray pointers and pointees. Pointees may not have the
  166. <code>ALLOCATABLE</code>, <code>INTENT</code>, <code>OPTIONAL</code>, <code>DUMMY</code>,
  167. <code>TARGET</code>, <code>INTRINSIC</code>, or <code>POINTER</code> attributes. Pointers
  168. may not have the <code>DIMENSION</code>, <code>POINTER</code>, <code>TARGET</code>,
  169. <code>ALLOCATABLE</code>, <code>EXTERNAL</code>, or <code>INTRINSIC</code> attributes, nor
  170. may they be function results. Pointees may not occur in more than one
  171. pointer statement. A pointee cannot be a pointer. Pointees cannot occur
  172. in equivalence, common, or data statements.
  173. </p>
  174. <p>A Cray pointer may also point to a function or a subroutine. For
  175. example, the following excerpt is valid:
  176. </p><div class="smallexample">
  177. <pre class="smallexample"> implicit none
  178. external sub
  179. pointer (subptr,subpte)
  180. external subpte
  181. subptr = loc(sub)
  182. call subpte()
  183. [...]
  184. subroutine sub
  185. [...]
  186. end subroutine sub
  187. </pre></div>
  188. <p>A pointer may be modified during the course of a program, and this
  189. will change the location to which the pointee refers. However, when
  190. pointees are passed as arguments, they are treated as ordinary
  191. variables in the invoked function. Subsequent changes to the pointer
  192. will not change the base address of the array that was passed.
  193. </p>
  194. <hr>
  195. <div class="header">
  196. <p>
  197. Next: <a href="CONVERT-specifier.html#CONVERT-specifier" accesskey="n" rel="next">CONVERT specifier</a>, Previous: <a href="Hollerith-constants-support.html#Hollerith-constants-support" accesskey="p" rel="prev">Hollerith constants support</a>, Up: <a href="Extensions-implemented-in-GNU-Fortran.html#Extensions-implemented-in-GNU-Fortran" accesskey="u" rel="up">Extensions implemented in GNU Fortran</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>
  198. </div>
  199. </body>
  200. </html>