1. The inline of GCC
GCC has made its own expansion of the C language, and its behavior differs greatly from the C99 standard.
1.1. Static inline
The static inline definition of GCC is easy to understand: you can think of it as a static function, plus the properties of inline. Most of this function behaves like a normal static function, except that when called, GCC expands its sink code at its invocation to compile without generating a separate assembly code for the function. In addition to the following situations:
When the address of the function is used. If the function is indirectly called through a function pointer. In this case, you have to generate a separate sink encoding for the static inline function, otherwise it does not have its own address.
Other things that cannot be unfolded, such as the behavior of the function itself, which recursively calls itself.
The static inline function, like the static function, defines a scope that is local, that is, you can have multiple definitions of the same name within a program (as long as it is not within the same file).
Attention
The performance behavior of the static inline of GCC is consistent with the static inline of the C99 standard. So this definition can be used with ease and without compatibility issues.
Points:
The static inline of GCC is only recommended for inline expansion of the compiler when invoked, relative to the static function;
GCC does not intentionally generate a separate sink encoding for the static inline function unless there is a situation where it must be generated (such as through function pointer invocation and recursive invocation);
The static inline function of GCC only works within the scope of the file.
1.2. Inline
Relative to the C99 of inline, GCC's inline is easier to understand: it can be thought of as an ordinary global function plus inline properties. That is, in the file where its definition resides, its performance is consistent with static inline: It is compiled by inline when it can be expanded. But in order to be able to use it outside of the file, GCC will certainly generate a separate sink code for it to be called externally. From the outside of the file, it is the same as a normal extern function. For example:
FOO.C:
/* This defines an inline function foo () */
Inline foo () {
...; the <-compiler will generate a separate assembler code
}
void Func1 () {
foo () as a non-inline function for foo (), <-with the file foo () The assembler code generated in the above file may be called directly by the compiler inline, instead of the assembly code
}
generated above the direct call, and the assembly number created in the other file:
bar.c:
extern foo (); <- declare foo (), note that you cannot have the inline keyword
void Func2 () {
foo () in the Declaration; <- Here is the assembler code that direct call generates for the Foo () function in FOO.C
}
Important:
The inline function of GCC is only called in the same file with respect to the normal extern function It is recommended that the compiler do inline expansion ;
Gcc It is bound to generate a separate assembler code for the inline function so that it is called outside of this file. In other files, the inline function is the same as the normal extern function; the inline function of
C is global: it can be expanded inline as an inline function in a file and can be called outside the file.
2. Inline in C + +
In C + +, in order to solve some of the frequently called small functions to consume a lot of stack space or is called stack memory problem, especially introduced the inline modifier, expressed as an inline function.
Points:
The use of inline is limited, and inline is only suitable for functions in which the code in the function body is simple, and cannot contain complex structure control statements such as while switch, and cannot inline the function itself cannot be a direct recursive function (it also calls its own function internally).
Define can do this inline work, but define is a side effect, especially for errors caused by different types of parameters, so inline has a stronger constraint and features that allow the compiler to check for more errors in C + + The use of define is not recommended.
Attached: #define和inline的区别
1 : Macro Define is completed during the preprocessing phase, and inline in the compile phase 2 : type safety Check the inline function is a function: to do type checking;3: Substitution method define string substitution; Inline is the embed code that does not generate code separately during compilation. Where the function is called is not a jump, but the code is written directly there, for the short function is more practical, and safe and reliable.
The inline function simply advises the compiler whether to replace it, and the compiler has the right to reject it.
GCC standard, inline in C + +