Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

174 lines
7.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>Labels as Values (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Labels as Values (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Labels as Values (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="Nested-Functions.html#Nested-Functions" rel="next" title="Nested Functions">
  31. <link href="Local-Labels.html#Local-Labels" rel="prev" title="Local Labels">
  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="Labels-as-Values"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Nested-Functions.html#Nested-Functions" accesskey="n" rel="next">Nested Functions</a>, Previous: <a href="Local-Labels.html#Local-Labels" accesskey="p" rel="prev">Local Labels</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="Labels-as-Values-1"></a>
  68. <h3 class="section">6.3 Labels as Values</h3>
  69. <a name="index-labels-as-values"></a>
  70. <a name="index-computed-gotos"></a>
  71. <a name="index-goto-with-computed-label"></a>
  72. <a name="index-address-of-a-label"></a>
  73. <p>You can get the address of a label defined in the current function
  74. (or a containing function) with the unary operator &lsquo;<samp>&amp;&amp;</samp>&rsquo;. The
  75. value has type <code>void *</code>. This value is a constant and can be used
  76. wherever a constant of that type is valid. For example:
  77. </p>
  78. <div class="smallexample">
  79. <pre class="smallexample">void *ptr;
  80. /* <span class="roman">&hellip;</span> */
  81. ptr = &amp;&amp;foo;
  82. </pre></div>
  83. <p>To use these values, you need to be able to jump to one. This is done
  84. with the computed goto statement<a name="DOCF3" href="#FOOT3"><sup>3</sup></a>, <code>goto *<var>exp</var>;</code>. For example,
  85. </p>
  86. <div class="smallexample">
  87. <pre class="smallexample">goto *ptr;
  88. </pre></div>
  89. <p>Any expression of type <code>void *</code> is allowed.
  90. </p>
  91. <p>One way of using these constants is in initializing a static array that
  92. serves as a jump table:
  93. </p>
  94. <div class="smallexample">
  95. <pre class="smallexample">static void *array[] = { &amp;&amp;foo, &amp;&amp;bar, &amp;&amp;hack };
  96. </pre></div>
  97. <p>Then you can select a label with indexing, like this:
  98. </p>
  99. <div class="smallexample">
  100. <pre class="smallexample">goto *array[i];
  101. </pre></div>
  102. <p>Note that this does not check whether the subscript is in bounds&mdash;array
  103. indexing in C never does that.
  104. </p>
  105. <p>Such an array of label values serves a purpose much like that of the
  106. <code>switch</code> statement. The <code>switch</code> statement is cleaner, so
  107. use that rather than an array unless the problem does not fit a
  108. <code>switch</code> statement very well.
  109. </p>
  110. <p>Another use of label values is in an interpreter for threaded code.
  111. The labels within the interpreter function can be stored in the
  112. threaded code for super-fast dispatching.
  113. </p>
  114. <p>You may not use this mechanism to jump to code in a different function.
  115. If you do that, totally unpredictable things happen. The best way to
  116. avoid this is to store the label address only in automatic variables and
  117. never pass it as an argument.
  118. </p>
  119. <p>An alternate way to write the above example is
  120. </p>
  121. <div class="smallexample">
  122. <pre class="smallexample">static const int array[] = { &amp;&amp;foo - &amp;&amp;foo, &amp;&amp;bar - &amp;&amp;foo,
  123. &amp;&amp;hack - &amp;&amp;foo };
  124. goto *(&amp;&amp;foo + array[i]);
  125. </pre></div>
  126. <p>This is more friendly to code living in shared libraries, as it reduces
  127. the number of dynamic relocations that are needed, and by consequence,
  128. allows the data to be read-only.
  129. This alternative with label differences is not supported for the AVR target,
  130. please use the first approach for AVR programs.
  131. </p>
  132. <p>The <code>&amp;&amp;foo</code> expressions for the same label might have different
  133. values if the containing function is inlined or cloned. If a program
  134. relies on them being always the same,
  135. <code>__attribute__((__noinline__,__noclone__))</code> should be used to
  136. prevent inlining and cloning. If <code>&amp;&amp;foo</code> is used in a static
  137. variable initializer, inlining and cloning is forbidden.
  138. </p>
  139. <div class="footnote">
  140. <hr>
  141. <h4 class="footnotes-heading">Footnotes</h4>
  142. <h3><a name="FOOT3" href="#DOCF3">(3)</a></h3>
  143. <p>The analogous feature in
  144. Fortran is called an assigned goto, but that name seems inappropriate in
  145. C, where one can do more than simply store label addresses in label
  146. variables.</p>
  147. </div>
  148. <hr>
  149. <div class="header">
  150. <p>
  151. Next: <a href="Nested-Functions.html#Nested-Functions" accesskey="n" rel="next">Nested Functions</a>, Previous: <a href="Local-Labels.html#Local-Labels" accesskey="p" rel="prev">Local Labels</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>
  152. </div>
  153. </body>
  154. </html>