📄 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.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
<!--
Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
<p>Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Funding
Free Software", the Front-Cover texts being (a) (see below), and with
the Back-Cover Texts being (b) (see below). A copy of the license is
included 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.-->
</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 program
which help the compiler optimize function calls and check your code more
carefully.
<p>The keyword <code>__attribute__</code> allows you to specify special
attributes when making a declaration. This keyword is followed by an
attribute specification inside double parentheses. The following
attributes 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>, and <code>nonnull</code>. Several other attributes are defined
for functions on particular target systems. Other attributes, 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 following
each keyword. This allows you to use them in header files without
being 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 using
attributes.
<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 define
their 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 what
would happen if <code>fatal</code> ever did return. This makes slightly
better code. More importantly, it helps avoid spurious warnings of
uninitialized variables.
<p>Do not assume that registers saved by the calling function are
restored before calling the <code>noreturn</code> function.
<p>It does not make sense for a <code>noreturn</code> function to have a return
type other than <code>void</code>.
<p>The attribute <code>noreturn</code> is not implemented in GCC versions
earlier than 2.5. An alternative way to declare that a function does
not return, which works in the current version and in some older
versions, 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 for
inlining.
<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 even
if no optimization level was specified.
<br><dt><code>pure</code>
<dd>Many functions have no effects except the return value and their
return value depends only on the parameters and/or global variables.
Such a function can be subject
to common subexpression elimination and loop optimization just as an
arithmetic operator would be. These functions should be declared
with 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 call
fewer 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 those
depending on volatile memory or other system resource, that may change between
two consecutive calls (such as <code>feof</code> in a multithreading environment).
<p>The attribute <code>pure</code> is not implemented in GCC versions earlier
than 2.96.
<br><dt><code>const</code>
<dd>Many functions do not examine any values except their arguments, and
have no effects except the return value. Basically this is just slightly
more strict class than the <code>pure</code> attribute above, since function is not
allowed to read global memory.
<p>Note that a function that has pointer arguments and examines the data
pointed to must <em>not</em> be declared <code>const</code>. Likewise, a
function 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 to
return <code>void</code>.
<p>The attribute <code>const</code> is not implemented in GCC versions earlier
than 2.5. An alternative way to declare that a function has no side
effects, 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 language
specifies 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 a
function cannot throw an exception. For example, most functions in
the standard C library can be guaranteed not to throw an exception
with the notable exceptions of <code>qsort</code> and <code>bsearch</code> that
take function pointer arguments. The <code>nothrow</code> attribute is not
implemented 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 which
should be type-checked against a format string. For example, the
declaration:
<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 is
interpreted, 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>.) The
parameter <var>string-index</var> specifies which argument is the format
string argument (starting from 1), while <var>first-to-check</var> is the
number of the first argument to check against the format string. For
functions where the arguments are not available to be checked (such as
<code>vprintf</code>), specify the third parameter as zero. In this case the
compiler only checks the format string for consistency. For
<code>strftime</code> formats, the third parameter is required to be zero.
<p>In the example above, the format string (<code>my_format</code>) is the second
argument of the function <code>my_print</code>, and the arguments to check
start with the third argument, so the correct parameters for the format
attribute are 2 and 3.
<p>The <code>format</code> attribute allows you to identify your own functions
which take format strings as arguments, so that GCC can check the
calls to these functions for errors. The compiler always (unless
<code>-ffreestanding</code> is used) checks formats
for 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 such
warnings are requested (using <code>-Wformat</code>), so there is no need to
modify 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 C
standard modes, the X/Open function <code>strfmon</code> is also checked as
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -