You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 line
8.4KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Nested Functions (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Nested Functions (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Nested Functions (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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="Nonlocal-Gotos.html#Nonlocal-Gotos" rel="next" title="Nonlocal Gotos">
  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.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Nested-Functions"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Nonlocal-Gotos.html#Nonlocal-Gotos" accesskey="n" rel="next">Nonlocal Gotos</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>
  65. </div>
  66. <hr>
  67. <a name="Nested-Functions-1"></a>
  68. <h3 class="section">6.4 Nested Functions</h3>
  69. <a name="index-nested-functions"></a>
  70. <a name="index-downward-funargs"></a>
  71. <a name="index-thunks"></a>
  72. <p>A <em>nested function</em> is a function defined inside another function.
  73. Nested functions are supported as an extension in GNU C, but are not
  74. supported by GNU C++.
  75. </p>
  76. <p>The nested function&rsquo;s name is local to the block where it is defined.
  77. For example, here we define a nested function named <code>square</code>, and
  78. call it twice:
  79. </p>
  80. <div class="smallexample">
  81. <pre class="smallexample">foo (double a, double b)
  82. {
  83. double square (double z) { return z * z; }
  84. return square (a) + square (b);
  85. }
  86. </pre></div>
  87. <p>The nested function can access all the variables of the containing
  88. function that are visible at the point of its definition. This is
  89. called <em>lexical scoping</em>. For example, here we show a nested
  90. function which uses an inherited variable named <code>offset</code>:
  91. </p>
  92. <div class="smallexample">
  93. <pre class="smallexample">bar (int *array, int offset, int size)
  94. {
  95. int access (int *array, int index)
  96. { return array[index + offset]; }
  97. int i;
  98. /* <span class="roman">&hellip;</span> */
  99. for (i = 0; i &lt; size; i++)
  100. /* <span class="roman">&hellip;</span> */ access (array, i) /* <span class="roman">&hellip;</span> */
  101. }
  102. </pre></div>
  103. <p>Nested function definitions are permitted within functions in the places
  104. where variable definitions are allowed; that is, in any block, mixed
  105. with the other declarations and statements in the block.
  106. </p>
  107. <p>It is possible to call the nested function from outside the scope of its
  108. name by storing its address or passing the address to another function:
  109. </p>
  110. <div class="smallexample">
  111. <pre class="smallexample">hack (int *array, int size)
  112. {
  113. void store (int index, int value)
  114. { array[index] = value; }
  115. intermediate (store, size);
  116. }
  117. </pre></div>
  118. <p>Here, the function <code>intermediate</code> receives the address of
  119. <code>store</code> as an argument. If <code>intermediate</code> calls <code>store</code>,
  120. the arguments given to <code>store</code> are used to store into <code>array</code>.
  121. But this technique works only so long as the containing function
  122. (<code>hack</code>, in this example) does not exit.
  123. </p>
  124. <p>If you try to call the nested function through its address after the
  125. containing function exits, all hell breaks loose. If you try
  126. to call it after a containing scope level exits, and if it refers
  127. to some of the variables that are no longer in scope, you may be lucky,
  128. but it&rsquo;s not wise to take the risk. If, however, the nested function
  129. does not refer to anything that has gone out of scope, you should be
  130. safe.
  131. </p>
  132. <p>GCC implements taking the address of a nested function using a technique
  133. called <em>trampolines</em>. This technique was described in
  134. <cite>Lexical Closures for C++</cite> (Thomas M. Breuel, USENIX
  135. C++ Conference Proceedings, October 17-21, 1988).
  136. </p>
  137. <p>A nested function can jump to a label inherited from a containing
  138. function, provided the label is explicitly declared in the containing
  139. function (see <a href="Local-Labels.html#Local-Labels">Local Labels</a>). Such a jump returns instantly to the
  140. containing function, exiting the nested function that did the
  141. <code>goto</code> and any intermediate functions as well. Here is an example:
  142. </p>
  143. <div class="smallexample">
  144. <pre class="smallexample">bar (int *array, int offset, int size)
  145. {
  146. __label__ failure;
  147. int access (int *array, int index)
  148. {
  149. if (index &gt; size)
  150. goto failure;
  151. return array[index + offset];
  152. }
  153. int i;
  154. /* <span class="roman">&hellip;</span> */
  155. for (i = 0; i &lt; size; i++)
  156. /* <span class="roman">&hellip;</span> */ access (array, i) /* <span class="roman">&hellip;</span> */
  157. /* <span class="roman">&hellip;</span> */
  158. return 0;
  159. /* <span class="roman">Control comes here from <code>access</code>
  160. if it detects an error.</span> */
  161. failure:
  162. return -1;
  163. }
  164. </pre></div>
  165. <p>A nested function always has no linkage. Declaring one with
  166. <code>extern</code> or <code>static</code> is erroneous. If you need to declare the nested function
  167. before its definition, use <code>auto</code> (which is otherwise meaningless
  168. for function declarations).
  169. </p>
  170. <div class="smallexample">
  171. <pre class="smallexample">bar (int *array, int offset, int size)
  172. {
  173. __label__ failure;
  174. auto int access (int *, int);
  175. /* <span class="roman">&hellip;</span> */
  176. int access (int *array, int index)
  177. {
  178. if (index &gt; size)
  179. goto failure;
  180. return array[index + offset];
  181. }
  182. /* <span class="roman">&hellip;</span> */
  183. }
  184. </pre></div>
  185. <hr>
  186. <div class="header">
  187. <p>
  188. Next: <a href="Nonlocal-Gotos.html#Nonlocal-Gotos" accesskey="n" rel="next">Nonlocal Gotos</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>
  189. </div>
  190. </body>
  191. </html>