PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/share/doc/gcc/Nested-Functions.html

https://gitlab.com/infected_/linaro_aarch64-linux-android-5.3.x
HTML | 213 lines | 151 code | 16 blank | 46 comment | 0 complexity | f14c289b0bdb4e967a9368a1e5e8ea85 MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2015 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>Using the GNU Compiler Collection (GCC): Nested Functions</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Nested Functions">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Nested Functions">
  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="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Constructing-Calls.html#Constructing-Calls" rel="next" title="Constructing Calls">
  31. <link href="Labels-as-Values.html#Labels-as-Values" rel="prev" title="Labels as Values">
  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="Nested-Functions"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Constructing-Calls.html#Constructing-Calls" accesskey="n" rel="next">Constructing Calls</a>, Previous: <a href="Labels-as-Values.html#Labels-as-Values" accesskey="p" rel="prev">Labels as Values</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</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="Nested-Functions-1"></a>
  69. <h3 class="section">6.4 Nested Functions</h3>
  70. <a name="index-nested-functions"></a>
  71. <a name="index-downward-funargs"></a>
  72. <a name="index-thunks"></a>
  73. <p>A <em>nested function</em> is a function defined inside another function.
  74. Nested functions are supported as an extension in GNU C, but are not
  75. supported by GNU C++.
  76. </p>
  77. <p>The nested function&rsquo;s name is local to the block where it is defined.
  78. For example, here we define a nested function named <code>square</code>, and
  79. call it twice:
  80. </p>
  81. <div class="smallexample">
  82. <pre class="smallexample">foo (double a, double b)
  83. {
  84. double square (double z) { return z * z; }
  85. return square (a) + square (b);
  86. }
  87. </pre></div>
  88. <p>The nested function can access all the variables of the containing
  89. function that are visible at the point of its definition. This is
  90. called <em>lexical scoping</em>. For example, here we show a nested
  91. function which uses an inherited variable named <code>offset</code>:
  92. </p>
  93. <div class="smallexample">
  94. <pre class="smallexample">bar (int *array, int offset, int size)
  95. {
  96. int access (int *array, int index)
  97. { return array[index + offset]; }
  98. int i;
  99. /* <span class="roman">&hellip;</span> */
  100. for (i = 0; i &lt; size; i++)
  101. /* <span class="roman">&hellip;</span> */ access (array, i) /* <span class="roman">&hellip;</span> */
  102. }
  103. </pre></div>
  104. <p>Nested function definitions are permitted within functions in the places
  105. where variable definitions are allowed; that is, in any block, mixed
  106. with the other declarations and statements in the block.
  107. </p>
  108. <p>It is possible to call the nested function from outside the scope of its
  109. name by storing its address or passing the address to another function:
  110. </p>
  111. <div class="smallexample">
  112. <pre class="smallexample">hack (int *array, int size)
  113. {
  114. void store (int index, int value)
  115. { array[index] = value; }
  116. intermediate (store, size);
  117. }
  118. </pre></div>
  119. <p>Here, the function <code>intermediate</code> receives the address of
  120. <code>store</code> as an argument. If <code>intermediate</code> calls <code>store</code>,
  121. the arguments given to <code>store</code> are used to store into <code>array</code>.
  122. But this technique works only so long as the containing function
  123. (<code>hack</code>, in this example) does not exit.
  124. </p>
  125. <p>If you try to call the nested function through its address after the
  126. containing function exits, all hell breaks loose. If you try
  127. to call it after a containing scope level exits, and if it refers
  128. to some of the variables that are no longer in scope, you may be lucky,
  129. but it&rsquo;s not wise to take the risk. If, however, the nested function
  130. does not refer to anything that has gone out of scope, you should be
  131. safe.
  132. </p>
  133. <p>GCC implements taking the address of a nested function using a technique
  134. called <em>trampolines</em>. This technique was described in
  135. <cite>Lexical Closures for C++</cite> (Thomas M. Breuel, USENIX
  136. C++ Conference Proceedings, October 17-21, 1988).
  137. </p>
  138. <p>A nested function can jump to a label inherited from a containing
  139. function, provided the label is explicitly declared in the containing
  140. function (see <a href="Local-Labels.html#Local-Labels">Local Labels</a>). Such a jump returns instantly to the
  141. containing function, exiting the nested function that did the
  142. <code>goto</code> and any intermediate functions as well. Here is an example:
  143. </p>
  144. <div class="smallexample">
  145. <pre class="smallexample">bar (int *array, int offset, int size)
  146. {
  147. __label__ failure;
  148. int access (int *array, int index)
  149. {
  150. if (index &gt; size)
  151. goto failure;
  152. return array[index + offset];
  153. }
  154. int i;
  155. /* <span class="roman">&hellip;</span> */
  156. for (i = 0; i &lt; size; i++)
  157. /* <span class="roman">&hellip;</span> */ access (array, i) /* <span class="roman">&hellip;</span> */
  158. /* <span class="roman">&hellip;</span> */
  159. return 0;
  160. /* <span class="roman">Control comes here from <code>access</code>
  161. if it detects an error.</span> */
  162. failure:
  163. return -1;
  164. }
  165. </pre></div>
  166. <p>A nested function always has no linkage. Declaring one with
  167. <code>extern</code> or <code>static</code> is erroneous. If you need to declare the nested function
  168. before its definition, use <code>auto</code> (which is otherwise meaningless
  169. for function declarations).
  170. </p>
  171. <div class="smallexample">
  172. <pre class="smallexample">bar (int *array, int offset, int size)
  173. {
  174. __label__ failure;
  175. auto int access (int *, int);
  176. /* <span class="roman">&hellip;</span> */
  177. int access (int *array, int index)
  178. {
  179. if (index &gt; size)
  180. goto failure;
  181. return array[index + offset];
  182. }
  183. /* <span class="roman">&hellip;</span> */
  184. }
  185. </pre></div>
  186. <hr>
  187. <div class="header">
  188. <p>
  189. Next: <a href="Constructing-Calls.html#Constructing-Calls" accesskey="n" rel="next">Constructing Calls</a>, Previous: <a href="Labels-as-Values.html#Labels-as-Values" accesskey="p" rel="prev">Labels as Values</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</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>
  190. </div>
  191. </body>
  192. </html>