⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nested-functions.html

📁 自己收集的linux入门到学懂高级编程书集 包括linux程序设计第三版
💻 HTML
字号:
<html lang="en"><head><title>Using the GNU Compiler Collection (GCC)</title><meta http-equiv="Content-Type" content="text/html"><meta name="description" content="Using the GNU Compiler Collection (GCC)"><meta name="generator" content="makeinfo 4.6"><!--Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.   <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below).  A copy of the license isincluded in the section entitled "GNU Free Documentation License".   <p>(a) The FSF's Front-Cover Text is:   <p>A GNU Manual   <p>(b) The FSF's Back-Cover Text is:   <p>You have freedom to copy and modify this GNU Manual, like GNU     software.  Copies published by the Free Software Foundation raise     funds for GNU development.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!--  pre.display { font-family:inherit }  pre.format  { font-family:inherit }  pre.smalldisplay { font-family:inherit; font-size:smaller }  pre.smallformat  { font-family:inherit; font-size:smaller }  pre.smallexample { font-size:smaller }  pre.smalllisp    { font-size:smaller }--></style></head><body><div class="node"><p>Node:&nbsp;<a name="Nested%20Functions">Nested Functions</a>,Next:&nbsp;<a rel="next" accesskey="n" href="Constructing-Calls.html#Constructing%20Calls">Constructing Calls</a>,Previous:&nbsp;<a rel="previous" accesskey="p" href="Labels-as-Values.html#Labels%20as%20Values">Labels as Values</a>,Up:&nbsp;<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a><hr><br></div><h3 class="section">Nested Functions</h3><p>A <dfn>nested function</dfn> is a function defined inside another function. (Nested functions are not supported for GNU C++.)  The nested function'sname is local to the block where it is defined.  For example, here wedefine a nested function named <code>square</code>, and call it twice:<pre class="smallexample">     foo (double a, double b)     {       double square (double z) { return z * z; }            return square (a) + square (b);     }     </pre>   <p>The nested function can access all the variables of the containingfunction that are visible at the point of its definition.  This iscalled <dfn>lexical scoping</dfn>.  For example, here we show a nestedfunction which uses an inherited variable named <code>offset</code>:<pre class="smallexample">     bar (int *array, int offset, int size)     {       int access (int *array, int index)         { return array[index + offset]; }       int i;       /* <small class="dots">...</small> */       for (i = 0; i &lt; size; i++)         /* <small class="dots">...</small> */ access (array, i) /* <small class="dots">...</small> */     }     </pre>   <p>Nested function definitions are permitted within functions in the placeswhere variable definitions are allowed; that is, in any block, beforethe first statement in the block.   <p>It is possible to call the nested function from outside the scope of itsname by storing its address or passing the address to another function:<pre class="smallexample">     hack (int *array, int size)     {       void store (int index, int value)         { array[index] = value; }            intermediate (store, size);     }     </pre>   <p>Here, the function <code>intermediate</code> receives the address of<code>store</code> as an argument.  If <code>intermediate</code> calls <code>store</code>,the arguments given to <code>store</code> are used to store into <code>array</code>. But this technique works only so long as the containing function(<code>hack</code>, in this example) does not exit.   <p>If you try to call the nested function through its address after thecontaining function has exited, all hell will break loose.  If you tryto call it after a containing scope level has exited, and if it refersto some of the variables that are no longer in scope, you may be lucky,but it's not wise to take the risk.  If, however, the nested functiondoes not refer to anything that has gone out of scope, you should besafe.   <p>GCC implements taking the address of a nested function using a techniquecalled <dfn>trampolines</dfn>.  A paper describing them is available as<a href="http://people.debian.org/~aaronl/Usenix88-lexic.pdf">http://people.debian.org/~aaronl/Usenix88-lexic.pdf</a>.   <p>A nested function can jump to a label inherited from a containingfunction, provided the label was explicitly declared in the containingfunction (see <a href="Local-Labels.html#Local%20Labels">Local Labels</a>).  Such a jump returns instantly to thecontaining function, exiting the nested function which did the<code>goto</code> and any intermediate functions as well.  Here is an example:<pre class="smallexample">     bar (int *array, int offset, int size)     {       __label__ failure;       int access (int *array, int index)         {           if (index &gt; size)             goto failure;           return array[index + offset];         }       int i;       /* <small class="dots">...</small> */       for (i = 0; i &lt; size; i++)         /* <small class="dots">...</small> */ access (array, i) /* <small class="dots">...</small> */       /* <small class="dots">...</small> */       return 0;           /* Control comes here from <code>access</code>         if it detects an error.  */      failure:       return -1;     }     </pre>   <p>A nested function always has internal linkage.  Declaring one with<code>extern</code> is erroneous.  If you need to declare the nested functionbefore its definition, use <code>auto</code> (which is otherwise meaninglessfor function declarations).<pre class="smallexample">     bar (int *array, int offset, int size)     {       __label__ failure;       auto int access (int *, int);       /* <small class="dots">...</small> */       int access (int *array, int index)         {           if (index &gt; size)             goto failure;           return array[index + offset];         }       /* <small class="dots">...</small> */     }     </pre>   </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -