📄 function-attributes.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 © 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: <a name="Function%20Attributes">Function Attributes</a>,Next: <a rel="next" accesskey="n" href="Attribute-Syntax.html#Attribute%20Syntax">Attribute Syntax</a>,Previous: <a rel="previous" accesskey="p" href="Mixed-Declarations.html#Mixed%20Declarations">Mixed Declarations</a>,Up: <a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a><hr><br></div><h3 class="section">Declaring Attributes of Functions</h3><p>In GNU C, you declare certain things about functions called in your programwhich help the compiler optimize function calls and check your code morecarefully. <p>The keyword <code>__attribute__</code> allows you to specify specialattributes when making a declaration. This keyword is followed by anattribute specification inside double parentheses. The followingattributes are currently defined for functions on all targets:<code>noreturn</code>, <code>noinline</code>, <code>always_inline</code>,<code>pure</code>, <code>const</code>, <code>nothrow</code>,<code>format</code>, <code>format_arg</code>, <code>no_instrument_function</code>,<code>section</code>, <code>constructor</code>, <code>destructor</code>, <code>used</code>,<code>unused</code>, <code>deprecated</code>, <code>weak</code>, <code>malloc</code>,<code>alias</code>, <code>warn_unused_result</code> and <code>nonnull</code>. Several otherattributes are defined for functions on particular target systems. Otherattributes, including <code>section</code> are supported for variables declarations(see <a href="Variable-Attributes.html#Variable%20Attributes">Variable Attributes</a>) and for types (see <a href="Type-Attributes.html#Type%20Attributes">Type Attributes</a>). <p>You may also specify attributes with <code>__</code> preceding and followingeach keyword. This allows you to use them in header files withoutbeing concerned about a possible macro of the same name. For example,you may use <code>__noreturn__</code> instead of <code>noreturn</code>. <p>See <a href="Attribute-Syntax.html#Attribute%20Syntax">Attribute Syntax</a>, for details of the exact syntax for usingattributes. <dl><dt><code>noreturn</code> <dd>A few standard library functions, such as <code>abort</code> and <code>exit</code>,cannot return. GCC knows this automatically. Some programs definetheir own functions that never return. You can declare them<code>noreturn</code> to tell the compiler this fact. For example, <pre class="smallexample"> void fatal () __attribute__ ((noreturn)); void fatal (/* <small class="dots">...</small> */) { /* <small class="dots">...</small> */ /* Print error message. */ /* <small class="dots">...</small> */ exit (1); } </pre> <p>The <code>noreturn</code> keyword tells the compiler to assume that<code>fatal</code> cannot return. It can then optimize without regard to whatwould happen if <code>fatal</code> ever did return. This makes slightlybetter code. More importantly, it helps avoid spurious warnings ofuninitialized variables. <p>The <code>noreturn</code> keyword does not affect the exceptional path when thatapplies: a <code>noreturn</code>-marked function may still return to the callerby throwing an exception. <p>Do not assume that registers saved by the calling function arerestored before calling the <code>noreturn</code> function. <p>It does not make sense for a <code>noreturn</code> function to have a returntype other than <code>void</code>. <p>The attribute <code>noreturn</code> is not implemented in GCC versionsearlier than 2.5. An alternative way to declare that a function doesnot return, which works in the current version and in some olderversions, is as follows: <pre class="smallexample"> typedef void voidfn (); volatile voidfn fatal; </pre> <br><dt><code>noinline</code> <dd>This function attribute prevents a function from being considered forinlining. <br><dt><code>always_inline</code> <dd>Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function evenif no optimization level was specified. <br><dt><code>pure</code> <dd>Many functions have no effects except the return value and theirreturn value depends only on the parameters and/or global variables. Such a function can be subjectto common subexpression elimination and loop optimization just as anarithmetic operator would be. These functions should be declaredwith the attribute <code>pure</code>. For example, <pre class="smallexample"> int square (int) __attribute__ ((pure)); </pre> <p>says that the hypothetical function <code>square</code> is safe to callfewer times than the program says. <p>Some of common examples of pure functions are <code>strlen</code> or <code>memcmp</code>. Interesting non-pure functions are functions with infinite loops or thosedepending on volatile memory or other system resource, that may change betweentwo consecutive calls (such as <code>feof</code> in a multithreading environment). <p>The attribute <code>pure</code> is not implemented in GCC versions earlierthan 2.96. <br><dt><code>const</code> <dd>Many functions do not examine any values except their arguments, andhave no effects except the return value. Basically this is just slightlymore strict class than the <code>pure</code> attribute above, since function is notallowed to read global memory. <p>Note that a function that has pointer arguments and examines the datapointed to must <em>not</em> be declared <code>const</code>. Likewise, afunction that calls a non-<code>const</code> function usually must not be<code>const</code>. It does not make sense for a <code>const</code> function toreturn <code>void</code>. <p>The attribute <code>const</code> is not implemented in GCC versions earlierthan 2.5. An alternative way to declare that a function has no sideeffects, which works in the current version and in some older versions,is as follows: <pre class="smallexample"> typedef int intfn (); extern const intfn square; </pre> <p>This approach does not work in GNU C++ from 2.6.0 on, since the languagespecifies that the <code>const</code> must be attached to the return value. <br><dt><code>nothrow</code> <dd>The <code>nothrow</code> attribute is used to inform the compiler that afunction cannot throw an exception. For example, most functions inthe standard C library can be guaranteed not to throw an exceptionwith the notable exceptions of <code>qsort</code> and <code>bsearch</code> thattake function pointer arguments. The <code>nothrow</code> attribute is notimplemented in GCC versions earlier than 3.2. <br><dt><code>format (</code><var>archetype</var><code>, </code><var>string-index</var><code>, </code><var>first-to-check</var><code>)</code> <dd>The <code>format</code> attribute specifies that a function takes <code>printf</code>,<code>scanf</code>, <code>strftime</code> or <code>strfmon</code> style arguments whichshould be type-checked against a format string. For example, thedeclaration: <pre class="smallexample"> extern int my_printf (void *my_object, const char *my_format, ...) __attribute__ ((format (printf, 2, 3))); </pre> <p>causes the compiler to check the arguments in calls to <code>my_printf</code>for consistency with the <code>printf</code> style format string argument<code>my_format</code>. <p>The parameter <var>archetype</var> determines how the format string isinterpreted, and should be <code>printf</code>, <code>scanf</code>, <code>strftime</code>or <code>strfmon</code>. (You can also use <code>__printf__</code>,<code>__scanf__</code>, <code>__strftime__</code> or <code>__strfmon__</code>.) Theparameter <var>string-index</var> specifies which argument is the formatstring argument (starting from 1), while <var>first-to-check</var> is thenumber of the first argument to check against the format string. Forfunctions where the arguments are not available to be checked (such as<code>vprintf</code>), specify the third parameter as zero. In this case thecompiler only checks the format string for consistency. For<code>strftime</code> formats, the third parameter is required to be zero. Since non-static C++ methods have an implicit <code>this</code> argument, thearguments of such methods should be counted from two, not one, whengiving values for <var>string-index</var> and <var>first-to-check</var>. <p>In the example above, the format string (<code>my_format</code>) is the secondargument of the function <code>my_print</code>, and the arguments to checkstart with the third argument, so the correct parameters for the formatattribute are 2 and 3. <p>The <code>format</code> attribute allows you to identify your own functionswhich take format strings as arguments, so that GCC can check thecalls to these functions for errors. The compiler always (unless<code>-ffreestanding</code> is used) checks formatsfor the standard library functions <code>printf</code>, <code>fprintf</code>,<code>sprintf</code>, <code>scanf</code>, <code>fscanf</code>, <code>sscanf</code>, <code>strftime</code>,<code>vprintf</code>, <code>vfprintf</code> and <code>vsprintf</code> whenever suchwarnings are requested (using <code>-Wformat</code>), so there is no need tomodify the header file <code>stdio.h</code>. In C99 mode, the functions<code>snprintf</code>, <code>vsnprintf</code>, <code>vscanf</code>, <code>vfscanf</code> and<code>vsscanf</code> are also checked. Except in strictly conforming Cstandard modes, the X/Open function <code>strfmon</code> is also checked asare <code>printf_unlocked</code> and <code>fprintf_unlocked</code>. See <a href="C-Dialect-Options.html#C%20Dialect%20Options">Options Controlling C Dialect</a>. <br><dt><code>format_arg (</code><var>string-index</var><code>)</code> <dd>The <code>format_arg</code> attribute specifies that a function takes a formatstring for a <code>printf</code>, <code>scanf</code>, <code>strftime</code> or<code>strfmon</code> style function and modifies it (for example, to translateit into another language), so the result can be passed to a<code>printf</code>, <code>scanf</code>, <code>strftime</code> or <code>strfmon</code> stylefunction (with the remaining arguments to the format function the sameas they would have been for the unmodified string). For example, thedeclaration: <pre class="smallexample"> extern char * my_dgettext (char *my_domain, const char *my_format) __attribute__ ((format_arg (2))); </pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -