source: public/doc/gnu-c/Inline-Function-Definitions.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.8 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Inline Function Definitions (GNU C Language Manual)</title>
23
24<meta name="description" content="Inline Function Definitions (GNU C Language Manual)">
25<meta name="keywords" content="Inline Function Definitions (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="Advanced-Definitions.html" rel="up" title="Advanced Definitions">
33<link href="Obsolete-Definitions.html" rel="next" title="Obsolete Definitions">
34<link href="Nested-Functions.html" rel="prev" title="Nested Functions">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Inline-Function-Definitions"></span><div class="header">
59<p>
60Previous: <a href="Nested-Functions.html" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced Definitions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="Inline-Function-Definitions-1"></span><h4 class="subsection">22.7.4 Inline Function Definitions</h4>
64<span id="index-inline-function-definitions"></span>
65<span id="index-function-definitions_002c-inline"></span>
66<span id="index-inline"></span>
67
68<p>To declare a function inline, use the <code>inline</code> keyword in its
69definition. Here&rsquo;s a simple function that takes a pointer-to-<code>int</code>
70and increments the integer stored there&mdash;declared inline.
71</p>
72<div class="example">
73<pre class="example">struct list
74{
75 struct list *first, *second;
76};
77
78inline struct list *
79list_first (struct list *p)
80{
81 return p-&gt;first;
82}
83
84inline struct list *
85list_second (struct list *p)
86{
87 return p-&gt;second;
88}
89</pre></div>
90
91<p>optimized compilation can substitute the inline function&rsquo;s body for
92any call to it. This is called <em>inlining</em> the function. It
93makes the code that contains the call run faster, significantly so if
94the inline function is small.
95</p>
96<p>Here&rsquo;s a function that uses <code>pair_second</code>:
97</p>
98<div class="example">
99<pre class="example">int
100pairlist_length (struct list *l)
101{
102 int length = 0;
103 while (l)
104 {
105 length++;
106 l = pair_second (l);
107 }
108 return length;
109}
110</pre></div>
111
112<p>Substituting the code of <code>pair_second</code> into the definition of
113<code>pairlist_length</code> results in this code, in effect:
114</p>
115<div class="example">
116<pre class="example">int
117pairlist_length (struct list *l)
118{
119 int length = 0;
120 while (l)
121 {
122 length++;
123 l = l-&gt;second;
124 }
125 return length;
126}
127</pre></div>
128
129<p>Since the definition of <code>pair_second</code> does not say <code>extern</code>
130or <code>static</code>, that definition is used only for inlining. It
131doesn&rsquo;t generate code that can be called at run time. If not all the
132calls to the function are inlined, there must be a definition of the
133same function name in another module for them to call.
134</p>
135<span id="index-inline-functions_002c-omission-of"></span>
136<p>Adding <code>static</code> to an inline function definition means the
137function definition is limited to this compilation module. Also, it
138generates run-time code if necessary for the sake of any calls that
139were not inlined. If all calls are inlined then the function
140definition does not generate run-time code, but you can force
141generation of run-time code with the option
142<samp>-fkeep-inline-functions</samp>.
143</p>
144<span id="index-extern-inline-function"></span>
145<p>Specifying <code>extern</code> along with <code>inline</code> means the function is
146external and generates run-time code to be called from other
147separately compiled modules, as well as inlined. You can define the
148function as <code>inline</code> without <code>extern</code> in other modules so as
149to inline calls to the same function in those modules.
150</p>
151<p>Why are some calls not inlined? First of all, inlining is an
152optimization, so non-optimized compilation does not inline.
153</p>
154<p>Some calls cannot be inlined for technical reasons. Also, certain
155usages in a function definition can make it unsuitable for inline
156substitution. Among these usages are: variadic functions, use of
157<code>alloca</code>, use of computed goto (see <a href="Labels-as-Values.html">Labels as Values</a>), and
158use of nonlocal goto. The option <samp>-Winline</samp> requests a warning
159when a function marked <code>inline</code> is unsuitable to be inlined. The
160warning explains what obstacle makes it unsuitable.
161</p>
162<p>Just because a call <em>can</em> be inlined does not mean it
163<em>should</em> be inlined. The GNU C compiler weighs costs and
164benefits to decide whether inlining a particular call is advantageous.
165</p>
166<p>You can force inlining of all calls to a given function that can be
167inlined, even in a non-optimized compilation. by specifying the
168&lsquo;<samp>always_inline</samp>&rsquo; attribute for the function, like this:
169</p>
170<div class="example">
171<pre class="example">/* <span class="roman">Prototype.</span> */
172inline void foo (const char) __attribute__((always_inline));
173</pre></div>
174
175<p>This is a GNU C extension. See <a href="Attributes.html">Attributes</a>.
176</p>
177<p>A function call may be inlined even if not declared <code>inline</code> in
178special cases where the compiler can determine this is correct and
179desirable. For instance, when a static function is called only once,
180it will very likely be inlined. With <samp>-flto</samp>, link-time
181optimization, any function might be inlined. To absolutely prevent
182inlining of a specific function, specify
183<code>__attribute__((__noinline__))</code> in the function&rsquo;s definition.
184</p>
185<hr>
186<div class="header">
187<p>
188Previous: <a href="Nested-Functions.html" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced Definitions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
189</div>
190
191
192
193</body>
194</html>
Note: See TracBrowser for help on using the repository browser.